[Bug]: Unexpected token 'export' when testEnvironment is `jsdom`
See original GitHub issueVersion
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:
- Created a year ago
- Comments:15
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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.
You need to remove
preset
in this case. Because when Jest mergespreset
andtransform
, yourtransform
config becomes thisSince your regex is not the same as the regex of the
preset
ints-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#L25Documentation 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.