7

I use @testing-library/react-native to test my RN app. When I run yarn test, following error occurs.

@testing-library/react-native should have "jest-preset.js" or "jest-preset.json" file at the root.

I use typescript for my app.

my test script is like this.

test": "jest --config jest.config.json"

jest.config.json file is like this.

{
  "preset": "@testing-library/react-native",
  "collectCoverageFrom": ["src/**/*.{ts,tsx}"],
  "moduleDirectories": ["node_modules", "src"],
  "setupFiles": [
    "<rootDir>/jestSetup/setup.js",
    "./node_modules/react-native-gesture-handler/jestSetup.js"
  ],
  "transformIgnorePatterns": [
    "node_modules/(?!(jest-)?(react-native|@?react-navigation|@react-native-community))"
  ],
  "coveragePathIgnorePatterns": ["/node_modules/", "/jestSetup", "/src/config.ts", "/src/app.tsx"]
}

Why am I getting this error?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Shashika Virajh
  • 8,497
  • 17
  • 59
  • 103

3 Answers3

4

I'm using expo and after updates from 38 to 39, Jest stopped working. I had same issues — it was complaining about missing preset js files.

Preset below didn't work for me:

"preset": "@testing-library/react-native",

So I have changed jest.config.js like this:

module.exports = {
clearMocks: true,
coverageDirectory: 'coverage',
testEnvironment: 'node',
preset: './node_modules/jest-expo/jest-preset.js',
}

Changed preset file location to expo's one which im using and it did the work

1

The preset no longer exists in >=7.0.0 "@testing-library/react-native". According to the documentation it seems "react-native" should be used as the preset.

  "preset": "react-native",

V7 upgrade documentation reference

fengelhardt
  • 965
  • 11
  • 24
0

For those seeing this in a fully up to date project, the missing file is [./node_modules/]react-native/jest-preset.js and you need to make sure that react-native itself is installed.

This will happen if you don't have react-native install globally.

yarn add react-native (or do it globally)

so in package.json you should see something like:

    "react-native": "0.66.3",