Unexpected token when using @testing-library/svelte
See original GitHub issueHi,
I’m new to svelte, and I’m trying to add some unit tests to my application. I followed explanations on how to setup the testing library, but when I execute the “npm test” command on a component that imports for exemple : import { link } from 'svelte-routing';, there is this error :
Jest encountered an unexpected token
Details:
~pathToMyProject/node_modules/svelte-routing/src/index.js:1
export { default as Router } from "./Router.svelte";
^^^^^^
SyntaxError: Unexpected token export
Could someone help me on this one ?
Thanks.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:7
- Comments:7 (1 by maintainers)
Top Results From Across the Web
vitest Svelte App with Typescript : ParseError ... Unexpected ...
I am trying to setup some unit-tests with vitest on a Svelte Application (with Typescript support), and I get the following error:
Read more >Setup Jest unit testing with Svelte | by Bill Kurtson | Medium
When I last tried to find how to unit test with Svelte at the end of 2019 it seemed that was ... Jest...
Read more >Example - Testing Library
// we have to wait for the next `tick` so that Svelte flushes all pending state changes. await fireEvent.click(button)
Read more >Jest Failure with importing packages (Typescript) : r/sveltejs
I have other test that are not using .svelte components that actually work ... Running Jest for Typescript CLI - unexpected token at...
Read more >Test Your Svelte Components with uvu and testing-library
(node:22333) UnhandledPromiseRejectionWarning: /redacted/uvu-svelte-testing-library/src/Counter.svelte:1 <script> ^ SyntaxError: Unexpected token '<'.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I was able to get past this error by adding a transformIgnorePatterns in my jest.config.js
jest.config.js
babel.config.js
However, this led to another error from
Link.svelteas follows:I came across this thread while looking for a solution but haven’t been able to solve it.
Problem
Link.svelteRouter.svelteIn the
Link.sveltecomponentROUTERgiven togetContext(ROUTER), will result in{ base }be assigned toundefined, assetContextfrom theRouter.sveltecomponent has not been used pass down the context objectROUTERtogetContext(ROUTER)TypeError: Cannot destructure property 'base' of 'getContext(...)' as it is undefined.Hence you’ll need to configure your tests to pass down a
RouterorMockRoutercomponent, to wrap aroundLink, during yourJest, including@testing-library/sveltetestsWorkaround
It’s not a nice workaround, as I’m currently using a package to convert my test files from
*.jsto*.jsxsolely to enable me to easily test components that are nested (same as I would in React). 😢npm install --save-dev svelte-jsx @babel/plugin-transform-react-jsxsvelte-jsxon how to test nested components - https://github.com/kenoxa/svelte-jsx#babel-configurationYou’d expect something like;