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.

v14.6.2 - jest 28 - React fails with 3rd party js

See original GitHub issue

Current Behavior

When running a test on a component that imports @headlessui/react, the transformer fails. While debugging, I saw that babel-jest transformer return an object with createTransform instead of a transformer.

Additionally - in our tests, we are using @swc/jest so I expected it to grab its transformer, not babel’s. Changing those lines to use @swc/jest makes it work.

Expected Behavior

Tests should not break.

Steps to Reproduce

  • Create a repo with React app
  • Install @headlessui/react
  • Import and use it in your component
  • Run jest

This issue may not be prioritized if details are not provided to help us reproduce the issue.

Failure Logs

 Test suite failed to run

    TypeError: transformer.process is not a function

      at Object.process (../../packages/react/plugins/jest.ts:37:43)
      at ScriptTransformer.transformSource (../../node_modules/@jest/transform/build/ScriptTransformer.js:619:31)
      at ScriptTransformer._transformAndBuildScript (../../node_modules/@jest/transform/build/ScriptTransformer.js:765:40)
      at ScriptTransformer.transform (../../node_modules/@jest/transform/build/ScriptTransformer.js:822:19)

Environment

 Node : 16.16.0
   OS   : darwin arm64
   yarn : 3.2.3
   
   nx : 14.6.2
   @nrwl/angular : Not Found
   @nrwl/cypress : 14.6.2
   @nrwl/detox : Not Found
   @nrwl/devkit : 14.6.2
   @nrwl/eslint-plugin-nx : 14.6.2
   @nrwl/express : Not Found
   @nrwl/jest : 14.6.2
   @nrwl/js : 14.6.2
   @nrwl/linter : 14.6.2
   @nrwl/nest : Not Found
   @nrwl/next : Not Found
   @nrwl/node : 14.6.2
   @nrwl/nx-cloud : 14.6.0
   @nrwl/nx-plugin : Not Found
   @nrwl/react : 14.6.2
   @nrwl/react-native : Not Found
   @nrwl/schematics : Not Found
   @nrwl/storybook : 14.6.2
   @nrwl/web : 14.6.2
   @nrwl/workspace : 14.6.2
   typescript : 4.7.4
   ---------------------------------------
   Local workspace plugins:
   ---------------------------------------
   Community plugins:
         rxjs: 6.6.7
         nx-stylelint: 13.5.3

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
danr-zacommented, Oct 21, 2022

I can confirm the code in the PR seems to solve the issue

0reactions
luchsamapparatcommented, Oct 17, 2022

I have the same issue in my React app but in my case it seems to be caused by @stencil/core:

      at Object.process (../../packages/react/plugins/jest.ts:37:43)
      at ScriptTransformer.transformSource (../../node_modules/@jest/transform/build/ScriptTransformer.js:619:31)
      at ScriptTransformer._transformAndBuildScript (../../node_modules/@jest/transform/build/ScriptTransformer.js:765:40)
      at ScriptTransformer.transform (../../node_modules/@jest/transform/build/ScriptTransformer.js:822:19)
      at Object.<anonymous> (../../node_modules/@stencil/core/internal/client/index.js:16:1)

It seems that require('babel-jest').default does not expose transform, only createTransformer (which when called would return an object with a transform method).

image

Here’s the commit that caused that incompatibility:

https://github.com/facebook/jest/commit/b096941c4a81e50ab2494f2909f4dcb300bdbab7#diff-5f463bc5dbe428998302335614e9bbed3b4eb3310a6075767e1b432c161d9e71L272

The corresponding PR: https://github.com/facebook/jest/pull/12407

So it seems to be caused by the update to Jest 28

Read more comments on GitHub >

github_iconTop Results From Across the Web

From v27 to v28 - Jest
Upgrading Jest from v27 to v28? This guide aims to help refactoring your configuration and tests. info. See changelog for the full list...
Read more >
SyntaxError: Cannot use import statement outside a module ...
When you use ECMAScript Modules in Node (v13.6.0 for me) combined with Jest, the following error occurs: SyntaxError: Cannot use import ...
Read more >
How to resolve "Cannot use import statement outside a ...
babel.config.js (only used by jest) ... For my case import issue was due to the react file drop by adding that to transformIgnorePatterns....
Read more >
Changelog - Cypress Documentation
Fixed an issue where projects using Angular v14.2.0 and above would be ... Removed eventemitter2 third-party type definitions from cy and Cypress that...
Read more >
How I Fixed The Unexpected Token Error In Jest
I recently ran into a nasty problem that took the better part of a day to get straight. So I'm gonna... Tagged with...
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