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?
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.
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.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With