Jest TransformIgnorePatterns all node_modules for React-Native Preset

juud5qan  于 2023-11-21  发布在  React
关注(0)|答案(3)|浏览(150)

我是开玩笑的新手,
在我的项目的- package.json中设置jest配置后,

Package.json

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

字符串
i已经尝试过忽略所有节点模块:-

"transformIgnorePatterns": ["node_modules"]


但对我不管用
和**.babelrc**

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


我的LoginScreen-Test.js Code:-

测试案例

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开始运行--> npm test或npm test-u
它反映了我以下错误:

终端输出

FAILtests/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预设。
寻找有用的答案…

42fyovps

42fyovps1#

此错误表明“react-native”尚未转换:react-native/Libraries/Utilities/Platform.ios.js:31
"transformIgnorePatterns": ["node_modules"]不会工作,因为它几乎是默认行为。
你试过使用官方推荐的配置吗?它应该看起来像你的项目:
"transformIgnorePatterns": [ "node_modules/(?!(react-native|react-native-cookies)/)" ]
?!很重要,因为它意味着忽略node_modules中的所有内容,除了react-nativereact-native-cookies

z9gpfhce

z9gpfhce2#

这是2023年的正确配置

"transformIgnorePatterns": [
     "node_modules/(?!((jest-)?react-native(-.*)?|@react-native(- 
     community)?)/)"
    ]

字符串

kupeojn6

kupeojn63#

我也有同样的问题,上面的方法对我不起作用。
jest.config.js

module.exports = {
  preset: '@testing-library/react-native',
  setupFilesAfterEnv: ['@testing-library/jest-native/extend-expect'],
  transformIgnorePatterns: [
    'node_modules/(?!((jest-)?react-native(-.*)?|@react-native(-community)?)/)',
  ],
  setupFiles: [
    './node_modules/react-native-gesture-handler/jestSetup.js',
    '<rootDir>/jest.setup.js',
  ],
  moduleNameMapper: {
    '\\.svg': '<rootDir>/__mocks__/svgMock.js',
  },
  collectCoverage: false,
  coverageDirectory: '__coverage__',
  coveragePathIgnorePatterns: ['/node_modules/', '.*.svg', '.*.png'],
}

字符串

相关问题