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 when importing publishable library.

See original GitHub issue

Current Behavior

I want to independently publish @example/a and @example/b. @example/b will have a dependency on @example/a.

When running jest on @example/b it fails to run due to ESM syntax.

https://github.com/nrwl/nx/blob/77b57b78e8f91ce1f2a3a3c674587909b405a76d/packages/jest/preset/jest-preset.ts#L12-L14

I updated the rollup config to produce a cjs build, however this did not help.

Expected Behavior

Tests should pass.

Steps to Reproduce

  1. git clone git@github.com:jamime/nx-jest-issue.git
  2. npm i
  3. npx nx test react-b

or

  1. npx create-nx-workspace example --preset react --appName demo
  2. npx nx g @nrwl/react:library react-a --publishable --importPath @example/a
  3. npx nx g @nrwl/react:library react-b --publishable --importPath @example/b
// libs/react-b/scr/lib/react-b.tsx
import styles from './react-b.module.css';
+ import { ReactA } from '@example/a';
/* eslint-disable-next-line */
export interface ReactBProps {}

export function ReactB(props: ReactBProps) {
  return (
    <div className={styles['container']}>
      <h1>Welcome to ReactB!</h1>
      <ReactA />
    </div>
  );
}

export default ReactB;
  1. npx nx test react-b

Failure Logs

> nx run react-b:test

 FAIL   react-b  libs/react-b/src/lib/react-b.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/jamime/nx/example/libs/react-a/src/index.ts:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export * from './lib/react-a';
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

      1 | import styles from './react-b.module.css';
    > 2 | import { ReactA } from '@example/a';
        | ^
      3 | /* eslint-disable-next-line */
      4 | export interface ReactBProps {}
      5 |

      at Runtime.createScriptFromCode (../../node_modules/jest-runtime/build/index.js:1796:14)
      at Object.<anonymous> (src/lib/react-b.tsx:2:1)
      at Object.<anonymous> (src/lib/react-b.spec.tsx:3:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.732 s
Ran all test suites.

Environment

   Node : 16.14.0
   OS   : darwin arm64
   npm  : 8.3.1
   
   nx : 14.8.2
   @nrwl/angular : Not Found
   @nrwl/cypress : 14.8.2
   @nrwl/detox : Not Found
   @nrwl/devkit : 14.8.2
   @nrwl/esbuild : Not Found
   @nrwl/eslint-plugin-nx : 14.8.2
   @nrwl/expo : Not Found
   @nrwl/express : Not Found
   @nrwl/jest : 14.8.2
   @nrwl/js : 14.8.2
   @nrwl/linter : 14.8.2
   @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.8.2
   @nrwl/react-native : Not Found
   @nrwl/rollup : 14.8.2
   @nrwl/schematics : Not Found
   @nrwl/storybook : 14.8.2
   @nrwl/web : 14.8.2
   @nrwl/webpack : 14.8.2
   @nrwl/workspace : 14.8.2
   typescript : 4.8.4
   ---------------------------------------
   Local workspace plugins:
   ---------------------------------------
   Community plugins:

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
barbados-clemenscommented, Nov 15, 2022

@pongells it looks like the react libraries weren’t updating the jest config but the apps were for nrwl/react.

fixed it here. https://github.com/nrwl/nx/pull/13175

0reactions
pongellscommented, Nov 15, 2022

Why is this issue closed? Shouldn’t new libraries created using the generators already have the correct name for the babel config file? And why is it working renaming it? 😕

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Jest test fails to run with ts-jest - Stack Overflow
test.tsx ○ Test suite failed to run Jest encountered an unexpected token This usually means that you are trying to import a file...
Read more >
nx jest unexpected token 'export' - You.com | The AI Search ...
Theory ; Jest encountered an unexpected token ; This usually means that you are trying to import a file which Jest cannot parse,...
Read more >
How I resolved issues while setting up Jest and Enzyme in a ...
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript. By default, if...
Read more >
Troubleshooting | jest-preset-angular - GitHub Pages
Unexpected token [import|export|other]​. This means, that a file is not transformed through TypeScript compiler, e.g. because it is a JS ...
Read more >
Configuring Jest
The configuration file should simply export an object: JavaScript; TypeScript. /** @type {import('jest').Config} */
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