Hello there. I am trying to setup jest for my expo app. While trying to execute a simple test, I get the following error:
src/pages/ProductsPage/ProductList/NewProductCard/__tests__/index.test.tsx
● Test suite failed to run
TypeError: Cannot read properties of null (reading 'match')
1 | import { store } from '@/store/index';
> 2 | import { registerRootComponent } from 'expo';
| ^
3 | import React, { useEffect, useState } from 'react';
4 | import { Provider } from 'react-redux';
5 | import { useLastNotificationResponse } from 'expo-notifications';
at match (node_modules/react-native/Libraries/Core/Devtools/getDevServer.js:31:29)
at Object.<anonymous> (node_modules/expo/src/environment/logging.fx.ts:15:39)
at Object.<anonymous> (node_modules/expo/src/Expo.fx.tsx:3:1)
at Object.<anonymous> (node_modules/expo/src/Expo.ts:1:1)
at Object.<anonymous> (index.js:2:1)
at Object.<anonymous> (src/pages/ProductsPage/ProductList/NewProductCard/__tests__/index.test.tsx:4:1)
import { menuResponse } from '@/__mocks__/menuResponse';
import { render } from '@testing-library/react-native';
import { NewProductCard } from '..';
describe('NewProductCard', () => {
it('should render the grouped product card', async () => {
render(<NewProductCard prod={menuResponse.products[0]} />);
});
});
This is a component inside a page using @react-navigation/native-stack.
Here’s my jest config in package.json:
"jest": {
"preset": "jest-expo",
"transformIgnorePatterns": [
"node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg)"
],
"moduleNameMapper": {
"\\.svg": "<rootDir>/src/__mocks__/svgMock.ts"
},
"setupFiles": [
"<rootDir>/jest/setup.js"
]
}
./jest/setup.js (as it says in the react navigation docs):
// include this line for mocking react-native-gesture-handler
import 'react-native-gesture-handler/jestSetup';