Skip to content

Tests Fail in New CLI Project Due to Jest 28 #33751

@CodingItWrong

Description

@CodingItWrong

Description

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/and processAsync() 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 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
  • 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 run yarn 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 and babel-jest to ^27, then rerun yarn 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution: FixedA PR that fixes this issue has been merged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions