Jest TransformIgnorePatterns React-Native Preset的所有node_modules

问题描述 投票:4回答:1

我是新的,是的,

在我的项目 - package.json中设置jest配置之后,

的package.json

"jest": {
    "preset": "react-native",
    "verbose": true,
    "moduleDirectories": ["node_modules", "src"],
    "transformIgnorePatterns": ["node_modules/(?!(react-native-cookies)/)"]
  },

我已经尝试忽略所有节点模块: -

"transformIgnorePatterns": ["node_modules"]

但不适合我

和.babelrc

{
  "presets": ["react-native"]
}

我的LoginScreen-Test.js代码: -

测试用例

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();
});

我开始跑 - > npm test或npm test - -u

它反映了我以下错误: -

终端输出

FAIL tests / LoginScreen-test.js●测试套件无法运行

/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)

我想通过TransformIgnorePattern忽略所有节点模块,但似乎它不适用于我的React-Native Preset,..

寻找有用的答案......

react-native jestjs
1个回答
4
投票

此错误表明“反应本机”尚未转换:react-native/Libraries/Utilities/Platform.ios.js:31

"transformIgnorePatterns": ["node_modules"]不起作用,因为它几乎是默认行为。

你尝试过使用official recommendation配置吗?对于您的项目,它看起来应该是这样的:

"transformIgnorePatterns": [ "node_modules/(?!(react-native|react-native-cookies)/)" ]

?!很重要,因为它意味着忽略node_modules中的所有内容,除了react-nativereact-native-cookies

© www.soinside.com 2019 - 2024. All rights reserved.