Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected token when using @testing-library/svelte #140

Open
samedro opened this issue May 11, 2020 · 7 comments
Open

Unexpected token when using @testing-library/svelte #140

samedro opened this issue May 11, 2020 · 7 comments

Comments

@samedro
Copy link

samedro commented May 11, 2020

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.

@EmilTholin
Copy link
Owner

Hi @samedro!

It's difficult to say what might have gone wrong with your test setup without seeing your project. Could you share a minimal reproducible example that we can ideally clone and try?

The issue is that your code has not been transpiled before testing and Jest doesn't know what to do with the export keyword.

@gushogg-blake
Copy link

gushogg-blake commented Jun 29, 2020

I'm also getting this issue with SSR. Does the code need to be transpiled for SSR?

Edit my mistake, I had an external rule in the Svelte build options causing svelte-routing not to get bundled.

@sonicoder86
Copy link

@EmilTholin the problem is Babel setups doesn't transpile files within node_modules. There is no build of the library for good old CommonJS.

@sonicoder86
Copy link

There is no build step before publishing, just the raw svelte and es6 syntax files.

@agrawal-rohit
Copy link

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.

@nhe23
Copy link

nhe23 commented Jan 3, 2021

@agrawal-rohit Having the same issue:

TypeError: Cannot destructure property 'base' of 'undefined' or 'null'.

  at instance (node_modules/svelte-routing/src/Link.svelte:105:8)
  at init (node_modules/svelte/internal/index.js:1474:11)
  at new Link (node_modules/svelte-routing/src/Link.svelte:236:3)
  at create_fragment (src/components/Nav/Nav.svelte:207:9)
  at init (node_modules/svelte/internal/index.js:1489:37)
  at new Nav (src/components/Nav/Nav.svelte:389:3)
  at Object.render (node_modules/@testing-library/svelte/dist/pure.js:81:21)
  at Object.<anonymous> (src/components/Nav/Nav.spec.ts:5:27)`

Have you found a solution?

@anthonytranDev
Copy link

anthonytranDev commented Jan 14, 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>,
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants