0

I want to use Jest with Puppeteer with TypeScript.

My Jest config is as follows:

{
  "transform": {
    "^.+\\.tsx?$": "ts-jest"
  },
  "globalSetup": "/home/mike/myapp/node_modules/jest-environment-puppeteer/setup.js",
  "globalTeardown": "/home/mike/myapp/node_modules/jest-environment-puppeteer/teardown.js",
  "testEnvironment": "/home/mike/myapp/node_modules/jest-environment-puppeteer/index.js",
  "setupFilesAfterEnv": [
    "/home/mike/myapp/node_modules/expect-puppeteer/lib/index.js"
  ],
  "testMatch": [
    "**/*.browser.test.ts"
  ]
}

When I run jest using this config, I get:


 FAIL  src/lib/settings.browser.test.ts
  ● Test suite failed to run

    Jest encountered an unexpected token

    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.

    (snip ...)

    Details:

    /home/mike/myapp/node_modules/uuid/dist/esm-browser/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { default as v1 } from './v1.js';
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1796:14)
      at Object.<anonymous> (node_modules/rpc-websockets/dist/lib/server.js:40:13)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        3.246 s
Ran all test suites.

I can see that Jest is complaining about the ES6 module syntax of node_modules/uuid/dist/esm-browser/index.js but I don't understand why.

  • Is the uuid module running in the browser? The version loaded is the dist/esm-browser version. Or is this node (which doesn't support ESM) out of the box - if so why is it loading dist/esm-browser?

  • If it's node, I have also tried NODE_OPTIONS=--experimental-vm-modules npx jest -c jest-browser.config.ts for ESM support in node, and this doesn't change the error.

How can I get jest running with Puppeteer using TypeScript?

mikemaccana
  • 110,530
  • 99
  • 389
  • 494

0 Answers0