9

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?

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

How does this work if I want to use the jest-expo/universal preset?
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

Comments

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",

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.