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.

[Bug]: Unexpected token 'export' when testEnvironment is `jsdom`

See original GitHub issue

Version

29.0.2

Steps to reproduce

check out https://github.com/rburgst/test-ts-jest

pnpm install
pnpm jest

You will get

    SyntaxError: Unexpected token 'export'

    > 1 | import { createRef } from 'preact'
        | ^
      2 |
      3 |
      4 | export function foo() {

      at Runtime.createScriptFromCode (node_modules/.pnpm/jest-runtime@29.0.3/node_modules/jest-runtime/build/index.js:1678:14)
      at Object.<anonymous> (src/index.ts:1:1)
      at Object.<anonymous> (src/index.test.ts:1:1)

I tried switching the whole project over to ESM but that showed a similar problem (see esm branch).

Expected behavior

the test should run successfully

Actual behavior

You will get

    SyntaxError: Unexpected token 'export'

    > 1 | import { createRef } from 'preact'
        | ^
      2 |
      3 |
      4 | export function foo() {

      at Runtime.createScriptFromCode (node_modules/.pnpm/jest-runtime@29.0.3/node_modules/jest-runtime/build/index.js:1678:14)
      at Object.<anonymous> (src/index.ts:1:1)
      at Object.<anonymous> (src/index.test.ts:1:1)

Debug log

that file is rather large

Additional context

Repro repo: https://github.com/rburgst/test-ts-jest

Environment

System:
    OS: macOS 12.6
    CPU: (10) arm64 Apple M1 Pro
  Binaries:
    Node: 16.17.0 - ~/.volta/tools/image/node/16.17.0/bin/node
    Yarn: 1.22.18 - ~/.volta/tools/image/yarn/1.22.18/bin/yarn
    npm: 8.15.0 - ~/.volta/tools/image/node/16.17.0/bin/npm
  npmPackages:
    jest: ^29.0.3 => 29.0.3

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
rburgstcommented, Oct 19, 2022

@ahnpnl thanks a lot for the explanation, with your config I can confirm that it works now. I am just flabbergasted that it takes so much configuration to get this to work when in ts-jest (and jest) 27 everything worked out of the box.

1reaction
ahnpnlcommented, Oct 3, 2022

it seems it ignores the tsconfig when inlined but used it if globals. however if globals, a warning to migrate is displayed with jest 29

module.exports = {
  preset: "ts-jest",
  globals: {
    "ts-jest": {
      tsconfig: {
        allowJs: true,
      },
    },
  },
  testEnvironment: "node",
  setupFiles: [`<rootDir>/test-setup.ts`],
  transform: {
    "^.+\\.(t|j)s$": "ts-jest",
  },
};

no issue but warning

ts-jest[ts-jest-transformer] (WARN) Define ts-jest config under globals is deprecated. Please do transform: { <transform_regex>: [‘ts-jest’, { /* ts-jest config goes here in Jest */ }], },

following solved warning

module.exports = {
  preset: "ts-jest",
  testEnvironment: "node",
  setupFiles: [`<rootDir>/test-setup.ts`],
  transform: {
    "^.+\\.(t|j)s$": ["ts-jest", {
      tsconfig: {
        allowJs: true,
      },
    }]
  },
};

but lead in my case to the error Unexpected token 'export' because the tsconfig is not intepreted

You need to remove preset in this case. Because when Jest merges preset and transform, your transform config becomes this

transform: {
    "^.+\\.(t|j)s$": ["ts-jest", {
      tsconfig: {
        allowJs: true,
      },
    }],
    "^.+\\.[tj]sx?$": "ts-jest"
  },

Since your regex is not the same as the regex of the preset in ts-jest repo, which Jest will produce that result which leads to your problem https://github.com/kulshekhar/ts-jest/blob/588c0d35d8099f261a56e736485178c817b62065/src/presets/create-jest-preset.ts#L25

Documentation already includes the regex patterns ts-jest uses https://kulshekhar.github.io/ts-jest/docs/getting-started/options I think documentation just needs a bit updates about this kind of merging behavior from Jest.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest gives an error: "SyntaxError: Unexpected token export"
This means, that a file is not transformed through TypeScript compiler, e.g. because it is a JS file with TS syntax, or it...
Read more >
Jest SyntaxError: Unexpected token 'export' #3443 - GitHub
After upgrading from 15.1.1 to 16.0.1 I'm getting the following error in my react unit tests. Any help would be much appreciated.
Read more >
How I Fixed The Unexpected Token Error In Jest
I refuse to write my packages with old-skool require() and module.export and all those other aging conventions. It's 2021, dammit. Babel isn't ...
Read more >
jest error syntaxerror: unexpected token 'export' - You.com
It appears to me that there is an issue in the package you're using. It's trying to use ES modules syntax (import /...
Read more >
Jest: Unexpected token 'export' - Sendbird Community
When I run my tests using Jest, I got an SyntaxError: Unexpected token 'export' from node_modules/@sendbird/chat/sendbird.js:1 .
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