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

After upgrading to jest 27: SyntaxError: Cannot use import statement outside a module #11446

Closed
doberkofler opened this issue May 26, 2021 · 13 comments

Comments

@doberkofler
Copy link

💥 Regression Report

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.

Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

By default "node_modules" folder is ignored by transformers.

Here's what you can do:
 • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation

Details:

/Users/doberkofler/MyDev/ljs_app/trunk/periscope/src/setup-jest.ts:4
import 'Source_ODS/jquery/jquery';
^^^^^^

SyntaxError: Cannot use import statement outside a module

  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1479:14)

Last working version

Worked up to version: 26.6.3

Stopped working in version: 27.0.1

To Reproduce

Steps to reproduce the behavior:

Expected behavior

Link to repl or repo (highly encouraged)

Run npx envinfo --preset jest

Paste the results here:

System:
    OS: macOS 11.3
    CPU: (8) x64 Intel(R) Core(TM) i7-8559U CPU @ 2.70GHz
  Binaries:
    Node: 16.1.0 - ~/.nvm/versions/node/v16.1.0/bin/node
    npm: 7.14.0 - ~/.nvm/versions/node/v16.1.0/bin/npm
  npmPackages:
    jest: 27.0.1 => 27.0.1
@SimenB
Copy link
Member

SimenB commented May 26, 2021

Please include a reproduction

@tu4mo
Copy link

tu4mo commented May 26, 2021

I also encountered this. Worked after jest --clearCache.

@doberkofler
Copy link
Author

I also encountered this. Worked after jest --clearCache.

@tu4mo Thank you for the tip, but already tried and it does not resolve the problem in my environment.

@doberkofler
Copy link
Author

@SimenB I've tried to reproduce the problem in a test environment but was not able to do so and therefore hoped that others might have the same problem and can help to track it down.
I've nevertheless attached my example
jest27.zip
to show the setup in my production environment.
The problem is reported in my setup-jest.ts file when import some legacy jQuery mockup files but does work in the example.

@doberkofler
Copy link
Author

@SimenB Could you eventually help me to better understand where the error actually originates or what the root cause might be?

@SimenB
Copy link
Member

SimenB commented May 26, 2021

seems more likely to be related to ts-jest. but I don't know

@arty-name
Copy link

Here’s a link to the same issue in ts-jest repo

@doberkofler
Copy link
Author

Here’s a link to the same issue in ts-jest repo

@arty-name Are you sure, this is the same problem? The error message looks quite different.

@doberkofler
Copy link
Author

@SimenB I now have an example to reproduce this problem and it seems to be caused by the "module": "esnext" setting in tsconfig.json
jest27.zip

@ahnpnl
Copy link
Contributor

ahnpnl commented May 26, 2021

This is issue with ts-jest :)

@SimenB
Copy link
Member

SimenB commented May 27, 2021

Ah cool (or not really, but you know 😅). I'll close this, then

@SimenB SimenB closed this as completed May 27, 2021
@arty-name
Copy link

@arty-name Are you sure, this is the same problem? The error message looks quite different.

True, it was a different issue, I skimmed it too quickly to notice that. For future reference I will now link to the new issue you have created there.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants