Jest encountered an unexpected token "export" in React
See original GitHub issueCurrent Behavior
The error Jest encountered an unexpected token
when writing a test for a library that depends on another library, coming from the export statement.
This is a fresh repository. https://github.com/kjrocker/nx-issue-repro
Expected Behavior
Unit tests should work for libraries that depend on other libraries by default.
Steps to Reproduce
With my repository:
Step 1: Clone https://github.com/kjrocker/nx-issue-repro
Step 2: Run npm install && npx nx test feature-lib
How I made my repository:
Created a TS workspace, installed @nrwl/react
, created two react libraries, base-lib
and feature-lib
.
Added the BaseLib
component as a child of the FeatureLib
component to create the dependency between them.
Ran the tests, which failed.
Failure Logs
> nx run feature-lib:test
FAIL feature-lib packages/feature-lib/src/lib/feature-lib.spec.tsx
β 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:
/Users/kevin.rocker/Developer/nx-test/packages/base-lib/src/index.ts:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export * from './lib/base-lib';
^^^^^^
SyntaxError: Unexpected token 'export'
> 1 | import { BaseLib } from '@nx-test/base-lib';
| ^
2 | import styles from './feature-lib.module.css';
3 |
4 | /* eslint-disable-next-line */
at Runtime.createScriptFromCode (../../node_modules/jest-runtime/build/index.js:1796:14)
at Object.<anonymous> (src/lib/feature-lib.tsx:1:1)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 1.217 s
Ran all test suites.
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
> NX Ran target test for project feature-lib (3s)
β 1/1 failed
β 0/1 succeeded [0 read from cache]
Environment
> NX Report complete - copy this into the issue template
Node : 16.16.0
OS : darwin x64
npm : 8.19.1
nx : 14.7.5
@nrwl/angular : Not Found
@nrwl/cypress : 14.7.5
@nrwl/detox : Not Found
@nrwl/devkit : 14.7.5
@nrwl/eslint-plugin-nx : 14.7.5
@nrwl/express : Not Found
@nrwl/jest : 14.7.5
@nrwl/js : 14.7.5
@nrwl/linter : 14.7.5
@nrwl/nest : Not Found
@nrwl/next : Not Found
@nrwl/node : Not Found
@nrwl/nx-cloud : Not Found
@nrwl/nx-plugin : Not Found
@nrwl/react : 14.7.5
@nrwl/react-native : Not Found
@nrwl/schematics : Not Found
@nrwl/storybook : 14.7.5
@nrwl/web : 14.7.5
@nrwl/workspace : 14.7.5
typescript : 4.8.3
---------------------------------------
Local workspace plugins:
---------------------------------------
Community plugins:
Issue Analytics
- State:
- Created a year ago
- Reactions:3
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Jest gives an error: "SyntaxError: Unexpected token export"
This means, that a file is not transformed through TypeScript compiler, e.g. because it is a JS file with TS syntax, or it...
Read more >Jest SyntaxError: Unexpected token 'export' #3443 - GitHub
After upgrading from 15.1.1 to 16.0.1 I'm getting the following error in my react unit tests. Any help would be much appreciated.
Read more >Jest SyntaxError: Unexpected token 'export'
For Webpack it is fine because it passes all the code through Babel, links all dependencies, and transpile them to vanilla js which...
Read more >Jest: Unexpected token 'export' - Sendbird Community
When I run my tests using Jest, I got an SyntaxError: Unexpected token 'export' from node_modules/@sendbird/chat/sendbird.js:1 .
Read more >jest export unexpected token - You.com | The AI Search ...
It appears to me that there is an issue in the package you're using. It's trying to use ES modules syntax (import /...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Using the transform configuration for @nrwl/react generated apps works.
It works. I wonder why this is not generated by nx?