Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React native jest.preset.js file missing error

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?

like image 719
Shashika Avatar asked Nov 15 '20 19:11

Shashika


People also ask

Why is my jest test not working in React Native?

This appears to have broken when the configuration for react-native was moved from the jest repo to the react-native repo, which I think is because the whatwg -based fix which was added to jest hasn't been copied across when it was removed from jest. Any test that uses fetch fails.

Does react-native jest preset provide an API?

therefore react-native jest preset (hosted in RN repository now) should also provide the API! Sorry, something went wrong. Since I posted my comment I was able to make it work. Production code doesn't define fetch, because otherwise the app would crash.

What is the React Native preset?

The preset is a node environment that mimics the environment of a React Native app. Because it doesn't load any DOM or browser APIs, it greatly improves Jest's startup time.

How to add jest-Expo to react native project?

After jest-expo is installed, open up package.json. Update package.json to include: 4. Your package.json should look like this: Install @testing-library/react-native and @testing-library/jest-native using yarn or npm. 1. In the root directory of the project, add a new file and name it jest.config.js. 2. Within jest.config.js, add this code: 3.


Video Answer


2 Answers

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

like image 97
Andras Terlecki Avatar answered Oct 21 '22 07:10

Andras Terlecki


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

like image 31
fengelhardt Avatar answered Oct 21 '22 08:10

fengelhardt