【问题标题】:Jest TransformIgnorePatterns all node_modules for React-Native PresetJest TransformIgnorePatterns 所有 node_modules 用于 React-Native Preset
【发布时间】:2018-10-27 12:58:00
【问题描述】:

我是开玩笑的新手,

在我的项目的 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 预设,..

寻找有用的答案...

【问题讨论】:

  • 你试过用--no-cache运行它
  • @AndreasKöberle 我也在我的 package.json 中尝试过这样的"test": "jest --no-cache"。但没有什么新的东西..
  • 看到和你一样的问题,使用 React Native 版本 0.56.0 和 Jest 23.6.0。除了一些“擦除您的依赖项并重新安装”之外,似乎在任何地方都没有答案......这对我没有帮助(当然甚至擦锁)

标签: react-native jestjs


【解决方案1】:

此错误表明“react-native”尚未转换: 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 除外。

【讨论】:

    猜你喜欢
    • 2017-12-20
    • 2019-08-29
    • 1970-01-01
    • 2019-02-01
    • 2022-07-10
    • 2019-05-17
    • 2019-02-19
    • 2021-07-09
    • 2022-01-01
    相关资源
    最近更新 更多