question-mark
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

Hi,

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:open
  • Created 3 years ago
  • Reactions:7
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
agrawal-rohitcommented, Aug 6, 2020

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

jest.config.js

module.exports = {
    transform: {
      '^.+\\.svelte$': 'svelte-jester',
      '^.+\\.js$': 'babel-jest',
    },
    transformIgnorePatterns: [
        "node_modules/?!(svelte-routing)"
      ],
    moduleFileExtensions: ['js', 'svelte'],
  }

babel.config.js

module.exports = {
    presets: [
      [
        '@babel/preset-env',
        {
          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.

0reactions
anthonytranDevcommented, Jan 19, 2021

Problem

Link.svelte

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

Router.svelte

...
  setContext(ROUTER, {
    activeRoute,
    base,
    routerBase,
    registerRoute,
    unregisterRoute
  });
</script>
...

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

Workaround

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 - https://github.com/kenoxa/svelte-jsx#babel-configuration

You’d expect something like;

render(
  <Router>
    <Link to="/"></Link>
  </Router>,
)
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 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 >

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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found