gpt4 book ai didi

react-native - Jest TransformIgnorePatterns 所有 node_modules 用于 React-Native Preset

转载 作者:行者123 更新时间:2023-12-04 17:43:24 24 4
gpt4 key购买 nike

我是 Jest 新手,

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

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

它向我反射(reflect)了以下错误:-

终端输出

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)


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

寻找有用的答案...

最佳答案

此错误表明“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-native 除外和 react-native-cookies .

关于react-native - Jest TransformIgnorePatterns 所有 node_modules 用于 React-Native Preset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50388367/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com