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.

Jest encountered an unexpected token "export" in React

See original GitHub issue

Current 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:open
  • Created a year ago
  • Reactions:3
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
sebastiandg7commented, Sep 28, 2022

Using the transform configuration for @nrwl/react generated apps works.

export default {
  displayName: 'my-lib',
  preset: '../../jest.preset.js',
  transform: {
    '^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nrwl/react/plugins/jest',
    '^.+\\.[tj]sx?$': ['babel-jest', { presets: ['@nrwl/react/babel'] }],
  },
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
  coverageDirectory: '../../coverage/libs/my-lib',
};

0reactions
ngoc199commented, Sep 28, 2022

Using the transform configuration for @nrwl/react generated apps works.

export default {
  displayName: 'my-lib',
  preset: '../../jest.preset.js',
  transform: {
    '^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nrwl/react/plugins/jest',
    '^.+\\.[tj]sx?$': ['babel-jest', { presets: ['@nrwl/react/babel'] }],
  },
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
  coverageDirectory: '../../coverage/libs/my-lib',
};

It works. I wonder why this is not generated by nx?

Read more comments on GitHub >

github_iconTop 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 >

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