nx: @nrwl/node library babel jest setting does not work
Current Behavior
After scaffolding a brand new node library using @nrwl/node and selecting the babelJest option tests do not run for the generated library.
Expected Behavior
Tests should be able to run in a new project
Steps to Reproduce
- Generate a new NX workspace
- Add the
@nrwl/nodepackage - Generate a node library choosing the
babelJestoption, eg:
npm run nx generate @nrwl/node:library – --name=babel-test
–buildable
–importPath=@some-org/babel-test
–publishable
–standaloneConfig
–strict
–no-interactive
–babelJest
- Run tests for the newly create library
npm run test babel-test:test - Observe that the tests fail to run
Failure Logs
FAIL babel-test packages/babel-test/src/lib/babel-test.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.
• 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:
/home/jesse/code/deepkit-community/modules/packages/babel-test/src/lib/babel-test.spec.ts:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { babelTest } from './babel-test';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Runtime.createScriptFromCode (../../node_modules/jest-runtime/build/index.js:1479:14)
at TestScheduler.scheduleTests (../../node_modules/@jest/core/build/TestScheduler.js:333:13)
Test Suites: 1 failed, 1 total Tests: 0 total Snapshots: 0 total Time: 0.218 s
Environment
Node : 14.15.4
OS : linux x64
npm : 6.14.14
nx : Not Found
@nrwl/angular : Not Found
@nrwl/cli : 12.7.2
@nrwl/cypress : Not Found
@nrwl/devkit : 12.7.2
@nrwl/eslint-plugin-nx : 12.7.2
@nrwl/express : Not Found
@nrwl/jest : 12.7.2
@nrwl/linter : 12.7.2
@nrwl/nest : Not Found
@nrwl/next : Not Found
@nrwl/node : 12.7.2
@nrwl/nx-cloud : 12.3.10
@nrwl/react : Not Found
@nrwl/schematics : Not Found
@nrwl/tao : 12.7.2
@nrwl/web : Not Found
@nrwl/workspace : 12.7.2
@nrwl/storybook : Not Found
@nrwl/gatsby : Not Found
typescript : 4.3.5
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 7
- Comments: 15 (6 by maintainers)
Commits related to this issue
- fix(testing): create root babel config for babel-jest option when creating a jest project with babel-jest a root babel.config.json file will be create if one doesn't exist for allowing jest tests to ... — committed to nrwl/nx by barbados-clemens 3 years ago
- fix(testing): create root babel config for babel-jest option when creating a jest project with babel-jest a root babel.config.json file will be create if one doesn't exist for allowing jest tests to ... — committed to nrwl/nx by barbados-clemens 3 years ago
- fix(testing): create root babel config for babel-jest option when creating a jest project with babel-jest a root babel.config.json file will be create if one doesn't exist for allowing jest tests to ... — committed to nrwl/nx by barbados-clemens 3 years ago
- fix(testing): create root babel config for babel-jest option when creating a jest project with babel-jest a root babel.config.json file will be create if one doesn't exist for allowing jest tests to ... — committed to nrwl/nx by barbados-clemens 3 years ago
- fix(testing): create root babel config for babel-jest option when creating a jest project with babel-jest a root babel.config.json file will be create if one doesn't exist for allowing jest tests to ... — committed to nrwl/nx by barbados-clemens 3 years ago
- fix(testing): create root babel config for babel-jest option when creating a jest project with babel-jest a root babel.config.json file will be create if one doesn't exist and babel deps install for... — committed to nrwl/nx by barbados-clemens 3 years ago
- fix(testing): create root babel config for babel-jest option when creating a jest project with babel-jest a root babel.config.json file will be create if one doesn't exist and babel deps install for... — committed to nrwl/nx by barbados-clemens 3 years ago
- fix(testing): create root babel config for babel-jest option when creating a jest project with babel-jest a root babel.config.json file will be create if one doesn't exist and babel deps install for... — committed to nrwl/nx by barbados-clemens 3 years ago
- fix(testing): create root babel config for babel-jest option when creating a jest project with babel-jest a root babel.config.json file will be create if one doesn't exist and babel deps install for... — committed to nrwl/nx by barbados-clemens 3 years ago
- fix(testing): create root babel config for babel-jest option when creating a jest project with babel-jest a root babel.config.json file will be create if one doesn't exist and babel deps install for... — committed to nrwl/nx by barbados-clemens 3 years ago
- fix(testing): Create root babel config if one isn't present (#7816) * fix(testing): create root babel config for babel-jest option when creating a jest project with babel-jest a root babel.config.... — committed to nrwl/nx by barbados-clemens 2 years ago
Any updates? I am also facing the same error.
@markogresak thanks!
The error disappears. I am now getting the
SyntaxError: Cannot use import statement outside a moduleError.I’ll try to do some config tweaking.
Adding@babel/preset-envnext to the original@nrwl/web/babelfixed the issue for me.Turns out adding the
@babel/preset-envis both wrong (can break the app) and unnecessary since the@nrwl/web/babelis already doing it.However, there is a special config for the test environment:
https://github.com/nrwl/nx/blob/cddb1edd5670e6ac7b106acc427dc23117cc2740/packages/web/babel.ts#L42-L43
nxdoes not set this automatically, you have to set it in your environment, for example:Tip: look into
cross-env(no affiliation) if you want cross-platform support for the env variable syntax above.@logitimate @valerius21 this issue (and probably the fix cc @barbados-clemens) is targeting
nrwl/node, notnrwl/angular. Are you having issue with Angular and Jest with Angular 13 / Nx 13? If so, please create a new issue and we can track there. Thanks!Running into this while updating to Angular 13 / Nx 13. I tried added that babel config at the root and making sure I have @nrwl/web installed but it doesn’t seem to be working still. Was there another step I needed to get around this for now?
Cool, thanks @barbados-clemens for looking into this! I’ll this out in the next little bit. Really excited at the possibility of dropping ts-jest, its just so slow
looking more into this, you’ll need to have
@nrwl/webinstalled if you don’t already as this is where the babel preset is coming from.@nrwl/web/babelas seen in the.babelrcfilebut it looks like
.babelrcisn’t being picked up when running tests via nx or jest. unless you run jest directly in the root of the library in question in which case it sees the.babelrcand the transforms run as expected. this is why it works in Webstorm as the current working directory is set to the specific library but not in nx or via jest in the root.Side Note: this looks to be only with buildable/publishable libraries, as I did not see the issue with only babel-jest enabled when generating a node library.
I’ll keep looking into this.
Edit: potentially related issue https://github.com/facebook/jest/issues/10647
Any help will be appreciated