Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest TransformIgnorePatterns all node_modules for React-Native Preset

Tags:

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...

like image 326
Lucifer_Latif Avatar asked May 17 '18 09:05

Lucifer_Latif


1 Answers

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.

like image 194
hfter Avatar answered Sep 17 '22 13:09

hfter