Tests Fail in New CLI Project Due to Jest 28
See original GitHub issueDescription
A new project generated with npx react-native init
is created with jest
and babel-jest
dependencies of version 28.x. This causes the generated __tests__/App-test.js
file to fail with errors including (full output further below)
- Invalid return value:
process()
or/andprocessAsync()
method of code transformer found at “…/node_modules/react-native/jest/assetFileTransformer.js” should return an object or a Promise resolving to an object. The object must havecode
property with a string of processed code. This error may be caused by a breaking change in Jest 28: https://jestjs.io/docs/upgrading-to-jest28#transformer - Jest did not exit one second after the test run has completed.
That error note about Jest 28 appears to be correct; downgrading to 27 fixes the errors.
I’d recommend that jest
and babel-jest
be locked to 27.x until all relevant dependencies are updated to work with 28.x
Version
0.68.1
Output of npx react-native info
System:
OS: macOS 12.3.1
CPU: (8) x64 Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
Memory: 8.48 GB / 32.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 16.14.2 - ~/.nvm/versions/node/v16.14.2/bin/node
Yarn: 1.22.18 - ~/.nvm/versions/node/v16.14.2/bin/yarn
npm: 8.7.0 - ~/.nvm/versions/node/v16.14.2/bin/npm
Watchman: 2022.03.21.00 - /usr/local/bin/watchman
Managers:
CocoaPods: 1.11.3 - /Users/josh/.rbenv/shims/pod
SDKs:
iOS SDK:
Platforms: DriverKit 21.4, iOS 15.4, macOS 12.3, tvOS 15.4, watchOS 8.5
Android SDK: Not Found
IDEs:
Android Studio: Not Found
Xcode: 13.3.1/13E500a - /usr/bin/xcodebuild
Languages:
Java: 17.0.2 - /usr/bin/javac
npmPackages:
@react-native-community/cli: Not Found
react: 17.0.2 => 17.0.2
react-native: 0.68.1 => 0.68.1
react-native-macos: Not Found
npmGlobalPackages:
*react-native*: Not Found
Steps to reproduce
- Create a new project with
npx react-native init YourProjectName
cd
into it and runyarn test
. You should see the following output, and the tests should hang:
console.error
The above error occurred in the <Header> component:
at Header (/Users/josh/apps/JestSandbox/node_modules/react-native/Libraries/NewAppScreen/components/Header.js:18:22)
at View
at Component (/Users/josh/apps/JestSandbox/node_modules/react-native/jest/mockComponent.js:28:18)
at RCTScrollView
at Component (/Users/josh/apps/JestSandbox/node_modules/react-native/jest/mockNativeComponent.js:17:18)
at ScrollViewMock (/Users/josh/apps/JestSandbox/node_modules/react-native/jest/mockScrollView.js:21:52)
at RCTSafeAreaView
at Component (/Users/josh/apps/JestSandbox/node_modules/react-native/jest/mockNativeComponent.js:17:18)
at SafeAreaView
at App (/Users/josh/apps/JestSandbox/App.js:56:22)
Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://reactjs.org/link/error-boundaries to learn more about error boundaries.
at logCapturedError (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:10989:23)
at update.callback (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11022:5)
at callCallback (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:3662:12)
at commitUpdateQueue (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:3683:9)
at commitLifeCycles (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11855:11)
at commitLayoutEffects (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:14443:7)
at Object.invokeGuardedCallbackProd (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11308:10)
FAIL __tests__/App-test.js (7.452 s)
✕ renders correctly (4963 ms)
● renders correctly
● Invalid return value:
`process()` or/and `processAsync()` method of code transformer found at
"/Users/josh/apps/JestSandbox/node_modules/react-native/jest/assetFileTransformer.js"
should return an object or a Promise resolving to an object. The object
must have `code` property with a string of processed code.
This error may be caused by a breaking change in Jest 28:
https://jestjs.io/docs/upgrading-to-jest28#transformer
Code Transformation Documentation:
https://jestjs.io/docs/code-transformation
at ScriptTransformer._buildTransformResult (node_modules/@jest/transform/build/ScriptTransformer.js:507:15)
at ScriptTransformer.transformSource (node_modules/@jest/transform/build/ScriptTransformer.js:628:17)
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 require (node_modules/react-native/Libraries/NewAppScreen/components/Header.js:23:15)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 7.527 s
Ran all test suites.
Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.
- Downgrade
jest
andbabel-jest
to^27
, then rerunyarn test
. You should see:
PASS __tests__/App-test.js (6.309 s)
✓ renders correctly (4430 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 6.372 s
Ran all test suites.
✨ Done in 8.19s.
Snack, code example, screenshot, or link to a repository
https://github.com/CodingItWrong/rn-cli-jest-28-errors demonstrates the issue. The main
branch is the default project created and shows the problem, and the jest27
branch downgrades to Jest 27 and shows the fix.
Issue Analytics
- State:
- Created a year ago
- Reactions:3
- Comments:10 (7 by maintainers)
Top Results From Across the Web
Jest 28: Shedding weight and improving compatibility
Sharding of test run. Jest now includes a new --shard CLI option, contributed by Mario Nebl. It allows you to run parts of...
Read more >Jest CLI Options
Run tests related to changed files based on hg/git (uncommitted files): ... of storing a new snapshot automatically, it will fail the test...
Read more >From v27 to v28 - Jest
This guide aims to help refactoring your configuration and tests. ... is a passing test. This has been changed in Jest 28 to...
Read more >Snapshot Testing - Jest
If we had any additional failing snapshot tests due to an unintentional bug, we would need to fix the bug before re-generating snapshots...
Read more >Jest · Delightful JavaScript Testing
Jest is a delightful JavaScript Testing Framework with a focus on simplicity. It works with projects using: Babel, TypeScript, Node, React, Angular, Vue...
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
Becuase is effectively off-topic to this issue, which has been resolved. The original issue is related to
npx react-native init
generating projects that depends on Jest 28. Your issue is unrelated to this. Please open a different issue.Hmm, although the output of my
npx react-native info
command above indicated that I didn’t havereact-native-cli
installed globally, I tried runningnpm uninstall react-native-cli
and then rerunningnpx react-native init RNLatest
, and then I get the Jest 26 output as you show. But now that I’ve uninstalled it, rerunningnpx react-native info
gives “Unrecognized command” so I can’t reconfirm the state now.In any case, I think that means this issue can be closed. Whatever CLI weirdness is going on, we’ve both confirmed that we see the correct Jest version outputted with no globally-installed CLI.