-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
React's babel/jest configuration is creating an error: Cannot use import statement outside a module #8323
Comments
Not sure if this is an issue with module.exports = {
...,
transform: {
'^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nrwl/react/plugins/jest',
'^.+\\.[tj]sx?$': 'ts-jest', // <- note `ts-jest` usage here _not_ `babel-jest`
},
...
} Not sure if that's an intentional change. This might also be a duplicate of #7844. Another related issue could be #8354. Definite head scratcher. Any insight the nx folks provide would be greatly appreciated. Will say the issue seems to be super finicky on my local. Fresh clone + install of my repo sometimes does run into the and other times a fresh clone doesn't 🤷♂️. |
I'm encountering a similar problem with my workspace in a Next.js app. I did some testing, and the problem form me stems from using ESModules (I think; I commented out all other code in my component). This is my error:
It seems for some reason my Jest config does not like things being imported from React (commenting out all React related imports such as |
Zero idea why this worked but I ended up blowing away my |
I am experiencing this as well. Testing a Next.JS App fails when importing a Lib that requires class properties transform. |
Very similar error: FAIL node-auth-functions libs/node-auth-functions/src/functions/acquire-access-token.spec.ts
● Test suite failed to run
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.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• 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:
C:\code\our-company\shared-packages\node_modules\node-fetch\src\index.js:9
import http from 'node:http';
^^^^^^
SyntaxError: Cannot use import statement outside a module
> 1 | import fetch, { Response } from 'node-fetch';
| ^
2 | import { IAccessTokenResponse } from '.';
3 | import { acquireAccessToken } from './acquire-access-token';
4 |
at Runtime.createScriptFromCode (../../node_modules/jest-runtime/build/index.js:1728:14)
at Object.<anonymous> (src/functions/acquire-access-token.spec.ts:1:1) In my case it seems to stem from the fact that a
P.S. I had to downgrade |
Is this issue fixed in PR #8075 ? |
If iirc nx does not generate a bablerc for next, probably because it (v12) uses SWC as a compiler? For libraries this is not the case but actually, every thing works fine for me if I pull my page into a separate library and then just render it like I would a normal component. |
I can confirm this does not work on NX 13.5.3. This is probably because the problem is for every @nrwl/react configuration, not for nextjs only. |
this might be related to #9640? can you add a preset to your root babel.config.json? |
I upgraded the reproduction repository to NX 14.1.7 and then added a preset to the root babel.config.json. However, the issue is still here. |
Any solution here? |
@airowe can you try adding the babel preset to the babel-jest transformer options like so |
Thanks @barbados-clemens! This got me working. Much appreciated. |
Hi @barbados-clemens, I updated the reproduction repository to use babel-jest instead of ts-jest, but the issue is still here. Do you have any idea of what I'm missing? |
I was able to get a somewhat different error.
by adding ky package
this tells jest to transform the ky packages since jest only uses common js and both those packages use ESM. but for whatever reason it's unable to run the tests because of this top level await even though we are using node v16 which has top level await support. if I patch the node_modules (and install the polyfills) it works as an fyi. maybe ask the maintainer of ky-univseral to see if they can patch it? though they shouldn't need to since toplevel await is like okay to use. I think this might be an issue elsewhere. globalThis.ReadableStream = (async () => await import('web-streams-polyfill/ponyfill/es2018'))(); different error is progress I guess? |
Indeed, this is good progress, it means it might work with other packages with ESM modules. |
This working for me |
I faced similar problem. This is minimal reporoduction repository. Error log.
Docker setup.
|
This line fix your issue |
@lordvins226 Finally, I found the cause of this error! When I changed env variable from This works for me.
|
@RNm-dove what is difference when you use NODE_ENV=test? I am having the same problem as you on my mac but using NODE_ENV=test does not help |
You can keep babel-jest but you also need to add
My file looks like this
replace my NX verison |
Managed to go around this issue by replacing the "test": {
"command": "NODE_OPTIONS=--experimental-vm-modules jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"..."
} |
I'm labeling this as blocked by upstream since the original issue is about the package 'ky-universal' and there seems to be an issue with how the output of the bundled code being created is using top level await which an for modules, but the package isn't specified to be a module from my understandings. as for the other packages most likely running into similar things or might be unrelated as noted by the mixed responses of things working for some and not others. If there is a helpful feedback on working with this package, 'ky-universal', or similar situations folks have been in with similar packages feel free to comment. otherwise will be best to open a new issue. |
This issue has been automatically marked as stale because it hasn't had any recent activity. It will be closed in 14 days if no further activity occurs. |
This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context. |
Hello,
Current Behavior
When running jest tests on a React component importing ky, the tests does not build because of
Jest encountered an unexpected token
error. Similar issues exist for @nrwl/angular and @nrwl/node, but none of the solution worked for @nrwl/react.I tried to fix the issue by modifying Jest configuration like on some other projects, but it didn't work:
Reading the other issues, I have a feeling the problem might come from babel-jest.
Expected Behavior
The tests should run.
Steps to Reproduce
Running
npm run test
on this repository:https://github.com/na-ji/nx-jest-transform
I managed to reproduce this error from nx 12 to the latest nx 13.
Failure Logs
Environment
Thanks.
The text was updated successfully, but these errors were encountered: