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.

Jest with ESM modules fails to parse headers-polyfill

See original GitHub issue

Prerequisites

Environment check

  • I’m using the latest msw version
  • I’m using Node.js version 14 or higher

Node.js version

v16.17.0

Reproduction repository

https://gist.github.com/earnubs/97bef90158e77cb8b646ab4586acf19d

Reproduction steps

npm test

Current behavior


    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • 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/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    /home/stephen/Workspace/try-jest-msw/node_modules/headers-polyfill/lib/esm/index.js:204
    export {
    ^^^^^^

    SyntaxError: Unexpected token 'export'

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1678:14)
          at async Promise.all (index 12)
          at async Promise.all (index 0)```

### Expected behavior

Test passes.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
earnubscommented, Oct 4, 2022

Adding type: module to headers-polyfill package.json does fix this for the repro case.

From https://nodejs.org/api/esm.html#esm_enabling

Authors can tell Node.js to use the ECMAScript modules loader via the .mjs file extension, the package.json “type” field, or the –input-type flag. Outside of those cases, Node.js will use the CommonJS module loader. See Determining module system for more details.

The esm output has .js file extensions, so maybe they need to be .mjs.

EDIT: yeah, changing all the esm extensions to mjs also fixes the repro, and that’s probably the correct fix in this case.

1reaction
earnubscommented, Oct 7, 2022

This may have squashed one bug only to reveal a few more with the ESM build:

The first one is related to the import of chalk at https://github.com/mswjs/msw/blob/main/src/node/createSetupServer.ts#L1

SyntaxError: The requested module 'chalk' does not provide an export named 'bold'

That’s easily fixed I think, but when fixed you hit:

Dynamic require of "node-fetch" is not supported

      at node_modules/msw/lib/node/index.mjs:25:9
      at node_modules/msw/src/context/fetch.ts:6:21

Which I think will be this line https://github.com/mswjs/msw/blob/main/src/context/fetch.ts#L6 and would require rewriting with something like await import ...

[5 mins later] OK, I see this PR https://github.com/mswjs/msw/pull/1399/files has all of what I’ve found so far, so I’ll try it out locally.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest fails to load jest.config.ts in a ESM project using ts-node 10
Error : Jest: Failed to parse the TypeScript config file ... import to load ES Module: /projects/ts-jest-ts-node-10/jest.config.ts require() ...
Read more >
ECMAScript Modules - Jest
Jest ships with experimental support for ECMAScript Modules (ESM). The implementation may have bugs and lack features. For the latest status ...
Read more >
Jest and ESM throwing errors with import fs - Stack Overflow
My package is quite simple, it does not use babel and it's plain javascript. I am getting a bit stuck here, because I...
Read more >
Jest runs with ESM modules... but not if they themselves have ...
Test suite failed to run Jest encountered an unexpected token Jest failed to parse a file. This happens e.g. when your code or...
Read more >
Open side panel - You.com
lib/sync' is not defined by "exports" in node_modules\csv-parse\package.json ... try configuring Jest to use ESM with this addition in jest.config.ts :.
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