question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@nrwl/node library babel jest setting does not work

See original GitHub issue

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/node package
  • Generate a node library choosing the babelJest option, 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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:7
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
smo043commented, Sep 11, 2021

Any updates? I am also facing the same error.

1reaction
valerius21commented, Nov 26, 2021

@markogresak thanks!

The error disappears. I am now getting the SyntaxError: Cannot use import statement outside a module Error.

I’ll try to do some config tweaking.

Read more comments on GitHub >

github_iconTop Results From Across the Web

jest encountered an unexpected token uuid - You.com | The AI ...
Jest encountered an unexpected token This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not...
Read more >
@nrwl/workspace:library | Nx
Useful for Node libraries that are not compiled by Babel. setParserOptionsProject. boolean. Default: false.
Read more >
nrwl-nx/community - Gitter
I'd like to do something like nx run project:target param1 param2 where ... By default, if Jest sees a Babel config, it will...
Read more >
Supported technologies: Nx/Nrwl - Wallaby.js
If jest does not run from the command-line for an application/library, you should be able to modify its jest.config.js to run by adding...
Read more >
Nx Angular: Jest encountered an unexpected token
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found