Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Jest Error]: Cannot use import statement outside a module #1210

Open
kouamschekina opened this issue Jan 22, 2025 · 0 comments
Open

[Jest Error]: Cannot use import statement outside a module #1210

kouamschekina opened this issue Jan 22, 2025 · 0 comments

Comments

@kouamschekina
Copy link

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:

  1. Create a new Node.js project with "type": "module" in package.json.
  2. Install jest.
  3. Create a simple test file using import statements.
  4. Run Jest with yarn jest
  5. 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.

Environment:

  • node -v: 18.20.4
  • yarn --version: 1.22.22
  • jest version: 29.7.0
  • Operating system: Ubuntu 22.04.1 LTS (Linux based)

Prerequisites

  • 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...

@kouamschekina kouamschekina changed the title [BUG]: Cannot use import statement outside a module [Jest Error]: Cannot use import statement outside a module Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant