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.

Support Testing with imports of JS files in ESM

See original GitHub issue

Current Behavior

When running test files with that contain ES6 module imports with Jest, I get the following error:

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

Is there an existing recommendation for handling ES6/ESNext module imports, if so, could we add documentation around this? If it doesn’t exist, could we add recommendation on how to handle it? I’m happy to help if I can. Could this be a TSDX feature, to switch on and off?

Desired Behavior

Either handled by default by TSDX, or there is clear recommendations on how best to handle this.

Suggested Solution

Happy to add docs with the recommendation to resolve, although I’m not sure what the recommendations are as yet.

Who does this impact? Who is this for?

Users importing other packages that use ES6.

Describe alternatives you’ve considered

I considered using babel-jest and configuring it myself, but this seems to make TSDX a little redundant.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:21 (8 by maintainers)

github_iconTop GitHub Comments

4reactions
karlhorkycommented, Apr 23, 2021

So this was fixed by my #486 very recently

I don’t believe this is fixed yet and this should be reopened.

Using the latest tsdx@0.14.1, Jest tests fail when they import something in ESM (eg. p-map) from node_modules, also when using the NODE_OPTIONS=--experimental-vm-modules option as described in the docs:

Repo: https://github.com/karlhorky/tsdx-test-esm-fail Repl.it (just hit the Run button at the top): https://replit.com/@karlhorky/tsdx-test-esm-fail

$ yarn test     
yarn run v1.22.10
$ tsdx test
 FAIL  test/blah.test.ts
  ● 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:

    tsdx-test-esm-fail/node_modules/p-map/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import AggregateError from 'aggregate-error';
                                                                                             ^^^^^^

    SyntaxError: Cannot use import statement outside a module

    > 1 | import pMap from 'p-map';
        | ^
      2 |
      3 | console.log('p-map', pMap);
      4 |

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1258:14)
      at Object.<anonymous> (src/index.ts:1:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.326s, estimated 2s
0reactions
lwazevedocommented, Feb 25, 2022

Hi,

Any solution to this problem?

I’m going through the same situation.

I have a third-party file in my project that internally imports two libs in js and when I run the test I get this error:

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

I solved it using @karlhorky suggestion documented in pr #1019, but I would like to know if this problem will be solved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What does it take to support Node.js ESM? – The Guild
There is a relatively small but effective way of automated testing for all the top-level imports in ESM, you can have an ESM...
Read more >
Getting Started with (and Surviving) Node.js ESM
1 - Resolving a simple import like "colors" to a file like "index.js" ... In May, 2020, Node.js v12.17.0 made ESM support available...
Read more >
ECMAScript Modules - Jest
Since ESM evaluates static import statements before looking at the code, the hoisting of jest.mock calls that happens in CJS won't work for...
Read more >
Jest and ESM throwing errors with import fs - Stack Overflow
Jest ships with experimental support for ECMAScript Modules (ESM). It is experimental and has many bugs. You need to transpile your ESM ......
Read more >
Using ES Modules (ESM) in Node.js: A Practical Guide (Part 3)
Mocha was actually the first test runner to support ESM (support written by yours truly ). So write your test files using ESM...
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