React native jest.preset.js file missing error
Asked Answered
S

3

8

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?

Spradling answered 15/11, 2020 at 19:13 Comment(0)
U
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

Upanddown answered 9/12, 2020 at 17:29 Comment(1)
How does this work if I want to use the jest-expo/universal preset?Onto
S
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

Skill answered 19/8, 2021 at 14:20 Comment(0)
B
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",
Barram answered 13/2, 2022 at 19:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.