You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
When running tests in my project using ES modules, Jest throws the following error: SyntaxError: Cannot use import statement outside a module.
This happens even after configuring Jest with transform and extensionsToTreatAsEsm. It seems that Jest cannot handle ES module syntax as expected in some configurations.
To Reproduce
Steps to reproduce the behavior:
Create a new Node.js project with "type": "module" in package.json.
Install jest.
Create a simple test file using import statements.
Run Jest with yarn jest
Observe the error: SyntaxError: Cannot use import statement outside a module.
Expected behavior
Jest should handle import statements correctly when configured to treat files as ES modules and transform them appropriately.
are you able to run jest from the command line? yes
where do you run jest CLI from? root directory of the project
how do you run your tests from the command line? yarn jest
Additional context
The issue persists even after trying common resolutions such as adding babel-jest, configuring transformIgnorePatterns, and treating extensions as ESM. Please let me know if additional steps or configurations could resolve this.
The fastest (and the most fun) way to resolve the issue is to submit a pull request yourself. If you are interested, please check out the contribution guide, we look forward to seeing your PR...
The text was updated successfully, but these errors were encountered:
kouamschekina
changed the title
[BUG]: Cannot use import statement outside a module
[Jest Error]: Cannot use import statement outside a module
Jan 22, 2025
Describe the bug
When running tests in my project using ES modules, Jest throws the following error:
SyntaxError: Cannot use import statement outside a module.
This happens even after configuring Jest with
transform
andextensionsToTreatAsEsm
. It seems that Jest cannot handle ES module syntax as expected in some configurations.To Reproduce
Steps to reproduce the behavior:
"type": "module"
in package.json.jest
.yarn jest
SyntaxError: Cannot use import statement outside a module.
Expected behavior
Jest should handle import statements correctly when configured to treat files as ES modules and transform them appropriately.
Environment:
node -v
: 18.20.4yarn --version
: 1.22.22Prerequisites
yarn jest
Additional context
The issue persists even after trying common resolutions such as adding babel-jest, configuring transformIgnorePatterns, and treating extensions as ESM. Please let me know if additional steps or configurations could resolve this.
The fastest (and the most fun) way to resolve the issue is to submit a pull request yourself. If you are interested, please check out the contribution guide, we look forward to seeing your PR...
The text was updated successfully, but these errors were encountered: