'Jest TransformIgnorePatterns all node_modules for React-Native Preset
I'm New to jest,
After setting-up jest config in my project's - package.json,
Package.json
"jest": {
"preset": "react-native",
"verbose": true,
"moduleDirectories": ["node_modules", "src"],
"transformIgnorePatterns": ["node_modules/(?!(react-native-cookies)/)"]
},
i already Tried this for ignoring all node modules:-
"transformIgnorePatterns": ["node_modules"]
But not working For me
and .babelrc
{
"presets": ["react-native"]
}
My LoginScreen-Test.js Code:-
TestCase
import 'react-native';
import React from 'react';
import LoginScreen from '../src/components/LoginScreen';
import renderer from 'react-test-renderer';
it('renders correctly', () => {
const hello = renderer.create(<LoginScreen/>).toJSON();
expect(hello).toMatchSnapshot();
});
i begin to run --> npm test or npm test -- -u
it reflects me with following error:-
Terminal Output
FAIL tests/LoginScreen-test.js ● Test suite failed to run
/Users/Documents/Projects/node_modules/react-native/Libraries/Utilities/Platform.ios.js:31 get isTesting(): boolean { ^ SyntaxError: Unexpected token : at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:316:17) at Object.get Platform [as Platform] (node_modules/react-native/Libraries/react-native/react-native-implementation.js:111:27) at Object.<anonymous> (node_modules/react-native-cookies/index.js:9:17)
I want to Ignore All Node modules by TransformIgnorePattern, but seems it is not Working for my React-Native Preset,..
Looking for the Helpful Answer...
Solution 1:[1]
This error shows that "react-native" has not been transformed:
react-native/Libraries/Utilities/Platform.ios.js:31
"transformIgnorePatterns": ["node_modules"] won't work because it is pretty much the default behavior.
Have you tried using the official recommendation config? It should look something like this for your project:
"transformIgnorePatterns": [
"node_modules/(?!(react-native|react-native-cookies)/)"
]
the ?! is important because it means ignore everything in the node_modules EXCEPT for react-native and react-native-cookies.
Solution 2:[2]
This is the correct config in 2022
"transformIgnorePatterns": [
"node_modules/(?!((jest-)?react-native(-.*)?|@react-native(-
community)?)/)"
]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | hfter |
| Solution 2 | Ridwan Ajibola |
