Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected token when using @testing-library/svelte

See original GitHub issue


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


 export { default as Router } from "./Router.svelte";

 SyntaxError: Unexpected token export

Could someone help me on this one ?


Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:7
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

agrawal-rohitcommented, Aug 6, 2020

I was able to get past this error by adding a transformIgnorePatterns in my jest.config.js


module.exports = {
    transform: {
      '^.+\\.svelte$': 'svelte-jester',
      '^.+\\.js$': 'babel-jest',
    transformIgnorePatterns: [
    moduleFileExtensions: ['js', 'svelte'],


module.exports = {
    presets: [
          targets: {
            node: 'current',

However, this led to another error from Link.svelte as follows:

TypeError: Cannot destructure property 'base' of 'getContext(...)' as it is undefined.

I came across this thread while looking for a solution but haven’t been able to solve it.

anthonytranDevcommented, Jan 19, 2021



const { base } = getContext(ROUTER); // <-- this will assigned undefined
const location = getContext(LOCATION); // <-- same here too!
const dispatch = createEventDispatcher();


  setContext(ROUTER, {

In the Link.svelte component ROUTER given to getContext(ROUTER), will result in { base } be assigned to undefined, as setContext from the Router.svelte component has not been used pass down the context object ROUTER to getContext(ROUTER)

TypeError: Cannot destructure property 'base' of 'getContext(...)' as it is undefined.

Hence you’ll need to configure your tests to pass down a Router or MockRouter component, to wrap around Link, during your Jest, including @testing-library/svelte tests


It’s not a nice workaround, as I’m currently using a package to convert my test files from *.js to *.jsx solely to enable me to easily test components that are nested (same as I would in React). 😢

  1. Follow @agrawal-rohit config setup
  2. Install the following npm install --save-dev svelte-jsx @babel/plugin-transform-react-jsx
  3. Read instructions in svelte-jsx on how to test nested components -

You’d expect something like;

    <Link to="/"></Link>
Read more comments on GitHub >

github_iconTop 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
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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found