Jest.js 在Expo应用程序中使用react-native-paper进行笑话测试

x6492ojm  于 2023-03-21  发布在  Jest
关注(0)|答案(1)|浏览(136)

我有一个世博会的应用程序,我正在设置。我已经添加了笑话测试,他们的工作很好。我想使用react-native-paper的样式,但当我添加,并修改一个页面,我到目前为止使用它的测试,我已经写了失败,由于导入的问题,我卡住了,直到如何修复它。
我的package.json如下所示

{
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "test": "jest",
    "web": "expo start --web",
    "lint": "eslint"
  },
  "jest": {
    "preset": "jest-expo",
    "setupFilesAfterEnv": [
      "@testing-library/jest-native/extend-expect"
    ],
    "transformIgnorePatterns": [
      "node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg)"
    ],
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx"
    ]
  },
  "dependencies": {
    "@expo/webpack-config": "^0.17.2",
    "@types/react": "~18.0.24",
    "@types/react-native": "~0.70.6",
    "expo": "~47.0.12",
    "expo-constants": "~14.0.2",
    "expo-status-bar": "~1.4.2",
    "firebase": "^9.17.1",
    "react": "18.1.0",
    "react-dom": "18.1.0",
    "react-native": "0.70.5",
    "react-native-paper": "^5.4.1",
    "react-native-safe-area-context": "^4.5.0",
    "react-native-web": "~0.18.9"
  },
  "devDependencies": {
    "@babel/core": "^7.12.9",
    "@expo/ngrok": "^4.1.0",
    "@testing-library/jest-native": "^5.4.2",
    "@testing-library/react-native": "^11.5.4",
    "@types/jest": "^29.4.0",
    "jest": "^26.6.3",
    "jest-expo": "^48.0.2"
  },
  "private": true
}

我的babel. config文件

module.exports = function(api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
    env: {
      production: {
        plugins: ['react-native-paper/babel'],
      },
    },
  };
};

当我运行测试时,我得到了以下错误

yarn run v1.22.17
$ jest
 FAIL  pages/Home.test.tsx
  ● Test suite failed to run

    TypeError: {omitted path}/node_modules/react-native-paper/lib/commonjs/index.js: Cannot read properties of undefined (reading '0')

      2 | import "@testing-library/jest-native/extend-expect";
      3 | import { render, screen } from "@testing-library/react-native"
    > 4 | import { Provider as PaperProvider } from 'react-native-paper';
        | ^

      at new SourceMap (../../node_modules/@babel/generator/src/source-map.ts:61:42)
      at new Generator (../../node_modules/@babel/generator/src/index.ts:24:35)
      at generate (../../node_modules/@babel/generator/src/index.ts:271:15)
      at generateCode (../../node_modules/@babel/core/src/transformation/file/generate.ts:35:22)
      at run (../../node_modules/@babel/core/src/transformation/index.ts:59:48)
          at run.next (<anonymous>)
      at transform (../../node_modules/@babel/core/src/transform.ts:29:20)
          at transform.next (<anonymous>)
      at evaluateSync (../../node_modules/gensync/index.js:251:28)
      at fn (../../node_modules/gensync/index.js:89:14)
      at stopHiding - secret - don't use this - v1 (../../node_modules/@babel/core/src/errors/rewrite-stack-trace.ts:97:14)
      at transformSync (../../node_modules/@babel/core/src/transform.ts:66:52)
      at ScriptTransformer.transformSource (../../node_modules/@jest/transform/build/ScriptTransformer.js:464:35)
      at ScriptTransformer._transformAndBuildScript (../../node_modules/@jest/transform/build/ScriptTransformer.js:569:40)
      at ScriptTransformer.transform (../../node_modules/@jest/transform/build/ScriptTransformer.js:607:25)
      at Object.<anonymous> (pages/Home.test.tsx:4:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.208 s
Ran all test suites.
error Command failed with exit code 1.

谢谢

icnyk63a

icnyk63a1#

我通过在package.json中的transformIgnorePatterns中添加react-native-paper修复了这个问题,现在看起来像这样:

"transformIgnorePatterns": [
      "node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg)|react-native-paper"
    ],

相关问题