Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest encountered an unexpected token when importing publishable library. #12360

Closed
jamime opened this issue Oct 1, 2022 · 8 comments
Closed
Labels
outdated scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx type: bug

Comments

@jamime
Copy link

jamime commented Oct 1, 2022

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.

* manually set the exports names to load in common js, to mimic the behaviors of jest 27
* before jest didn't fully support package exports and would load in common js code (typically via main field). now jest 28+ will load in the browser esm code, but jest esm support is not fully supported.
* In this case we will tell jest to load in the common js code regardless of environment.

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:
@jamime
Copy link
Author

jamime commented Oct 2, 2022

This change was sufficient to get the tests passing, should this be the default behaviour?

//libs/react-b/jest.config.ts
/* eslint-disable */
export default {
  displayName: 'react-b',
  preset: '../../jest.preset.js',
  transform: {
-     '^.+\\.[tj]sx?$': 'babel-jest',
+    '^.+\\.[tj]sx?$': ['babel-jest', { presets: ['@nrwl/react/babel'] }],
  },
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
  coverageDirectory: '../../coverage/libs/react-b',
};

@AgentEnder AgentEnder added the scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx label Oct 4, 2022
@barbados-clemens
Copy link
Contributor

This is the default behavior as of v14.6.0 and a migration was provided at that point to update it. Can you confirm this migration ran for you workspace?

https://github.com/nrwl/nx/blob/master/packages/react/src/utils/jest-utils.ts

https://github.com/nrwl/nx/blob/master/packages/react/migrations.json#63

@jamime
Copy link
Author

jamime commented Oct 5, 2022

This was a brand new repository created with 14.8.2.

@pongells
Copy link
Contributor

pongells commented Oct 26, 2022

I am on Nx 15.0.3.

When generating a new lib with the React generator:
nx g @nrwl/react:lib my-lib

I still get the wrong transform expression, I guess:

  transform: {
    "^.+\\.[tj]sx?$": "babel-jest",
  },

The only place where the transform is the one suggested above, is the apps. All the libraries I generated have the old one.

@puku0x
Copy link
Contributor

puku0x commented Nov 5, 2022

Solved by renaming the Babel configuration file babel.config.json.

image

  • babel.config.json
{
  "presets": [
    [
      "@nrwl/react/babel",
      {
        "runtime": "automatic",
        "useBuiltIns": "usage"
      }
    ]
  ],
  "plugins": []
}
  • jest.config.ts
/* eslint-disable */
export default {
  displayName: 'react-b',
  preset: '../../jest.preset.js',
  transform: {
    '^.+\\.[tj]sx?$': 'babel-jest',
  },
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
  coverageDirectory: '../../coverage/libs/react-b',
};
  • Result

image

@pongells
Copy link
Contributor

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? :/

@barbados-clemens
Copy link
Contributor

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

fixed it here.
#13175

@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx type: bug
Projects
None yet
Development

No branches or pull requests

6 participants
@jamime @pongells @puku0x @AgentEnder @barbados-clemens and others