I am running a test for a component that needs to check if it has a particular CSS style. As the React Native Testing Library doesn’t have this function by default, I installed the @testing-library/react-native to use toHaveStyle from there but while running a test I get an error: Test suite failed to run. Cannot find module ‘@testing-library/jest-native’ from "a path to my test file here". Here is my test and the jest config:
// test file
import React from 'react';
import {toHaveStyle} from '@testing-library/jest-native';
describe('JobForm', () => {
expect.extend({toHaveStyle});
// ....
});
// package.json
{
//...
"jest": {
"preset": "react-native",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
],
"transformIgnorePatterns": [
"node_modules/(?!(jest-)?@?react-native|@react-native-community|@react-navigation|aws-amplify-react-native|@ui-kitten)"
],
"setupFiles": [
"<rootDir>/jest.setup.js",
"./node_modules/react-native-gesture-handler/jestSetup.js"
]
}
}
//jest.setup.js
import mockRNCNetInfo from '@react-native-community/netinfo/jest/netinfo-mock.js';
import mockAsyncStorage from '@react-native-async-storage/async-storage/jest/async-storage-mock';
jest.mock('@react-native-community/netinfo', () => mockRNCNetInfo);
jest.mock('@react-native-async-storage/async-storage', () => mockAsyncStorage);
2
Answers
It seems that you forgot to finish the configuration from the Usage section (a section below the Installation section) from the
@testing-library/jest-native
package. All you need to do is add this line to your setup file:In case of config or import issues it’s always a good idea to compare you config against model one provided by RNTL team. RNTL has a basic example app that is useful for that purpose, especially that it also includes
@testing-library/jest-native
.