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.

[v5.0.0] Fails jest tests

See original GitHub issue

I am using url-join on a frontend project. I believe the browser have trouble understanding the export statement in lib. When I revert back to v4.1.0 all works well.

This was the error I was seeing in my jest tests.

 FAIL  tests/unit/App.spec.js
  ● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    /node_modules/url-join/lib/url-join.js:60
    export default function urlJoin() {
    ^^^^^^

    SyntaxError: Unexpected token 'export'

    > 1 | import urlJoin from 'url-join';
        | ^

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
      at Object.<anonymous> (src/services/client.js:1:1)

Test Suites: 1 failed, 2 passed, 3 total
Tests:       25 passed, 25 total
Snapshots:   0 total
Time:        4.948s
Ran all test suites.                          

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
david0178418commented, Apr 8, 2022

@dennismphil For you and others running into issues on this front, if you take a look at the code, the module is actually quite small and easily lifted out and copied into your project.

If you look at the PR that changed the module format, there was a link to this thread which has since been “locked”. Copying this out for the time being until such time that your project might be able to be adapted to work with native node esm modules will get you unblocked while dodging the crossfire in what has become an oddly ideological holy war.

1reaction
tonycommented, Mar 26, 2022

@jonkoops This new distribution method does make certain things more difficult.

As an example I use gatsby, I get stung by this here: https://github.com/gatsbyjs/gatsby/issues/23705. This is tricky.

Maybe I’m the only one, but maybe it could be more portable to (some?) users if the package added a distribution for commonjs.

🤷 It’s pretty easy to include an extra build output, to include esm, commonjs, etc. (rollup, webpack, tsc). It’s your call, though

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest fails with CRA v5.0.0 #12157 - facebook/create-react-app
react-scripts test command fails after updating the project to CRA (react-scripts) version 5.0.0. In previous versions it was working fine.
Read more >
Jest-dom give the error "TypeError: expect(...).toHaveStyle is ...
0.0, you should use import '@testing-library/jest-dom';. You didn't add the matchers for expect correctly. That's the reason you get the error.
Read more >
Testing Asynchronous Code - Jest
Return a promise from your test, and Jest will wait for that promise to resolve. If the promise is rejected, the test will...
Read more >
Testing Relay Components
The purpose of this document is to cover the Relay APIs for testing Relay components. The content is focused mostly on jest unit-tests...
Read more >
Changelog - Cypress Documentation
Fixed an issue where the Cypress migration wizard would fail to run in global ... 5.0.0. Released 8/19/2020. Summary: Cypress now includes support...
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