1

运行时出现此错误npm test。我查看了许多建议对 , 进行编辑的帖子、解决方案等,package.jsonbabel.config.jsjest.config.js仍然收到此错误。我认为这可能是 React Native 的版本问题,但我使用的是最新版本 0.60.4,并且在一些问题中提到这在 master 中已解决。也许我忽略了一些东西,但我对它可能是什么还不够。

以下是因此失败的测试:

import { geolocationRequest } from '../location';
let mockGeoCoding = jest.fn();

jest.mock('react-native-geocoding', () => ({
    openURL: mockGeoCoding,
}));

describe('geolocationRequest', () => {
  it('creates a properly formatted action', () => {
    expect(geolocationRequest()).toMatchSnapshot();
  })
})

想法?

包.json

{
      "name": "<app-name>",
      "version": "0.0.1",
      "private": true,
      "scripts": {
        "start": "node node_modules/react-native/local-cli/cli.js start --config ../../../../rn-cli.config.js",
        "start:ios": "react-native run-ios",
        "start:android": "react-native run-android",
        "clean:ios": "cd ios/ && pod deintegrate && pod install",
        "clean:android": "cd android && ./gradlew clean",
        "test": "jest --watch",
        "test:coverage": "jest --coverage && open coverage/lcov-report/index.html",
        "fix": "eslint --fix .",
        "lint:ts": "tslint --fix --project ./tsconfig.json",
        "lint:fix": "npm run lint:ts --fix",
        "lint:check": "tslint --type-check --project"
      },
      "dependencies": {
        "@react-native-community/async-storage": "1.4.2",
        "appcenter": "1.12.2",
        "appcenter-analytics": "1.12.2",
        "appcenter-crashes": "1.12.2",
        "axios": "^0.19.0",
        "expo-font": "4.0.0",
        "polyline": "0.2.0",
        "react": "16.8.6",
        "react-devtools": "3.6.1",
        "react-native": "0.60.*",
        "react-native-animatable": "1.3.2",
        "react-native-auth0": "1.4.2",
        "react-native-easy-grid": "0.2.1",
        "react-native-elements": "1.1.0",
        "react-native-geocoding": "0.3.0",
        "react-native-gesture-handler": "1.3.0",
        "react-native-maps": "0.24.2",
        "react-native-vector-icons": "6.4.2",
        "react-navigation": "3.9.1",
        "react-redux": "7.1.0",
        "redux": "4.0.0",
        "redux-thunk": "2.3.0",
        "scheduler": "0.14.0"
      },
      "devDependencies": {
        "@babel/plugin-proposal-class-properties": "^7.5.5",
        "@babel/preset-typescript": "^7.3.3",
        "@types/enzyme": "^3.10.3",
        "@types/enzyme-adapter-react-16": "^1.0.5",
        "@types/jest": "^24.0.17",
        "@types/polyline": "0.1.28",
        "@types/react": "16.8.13",
        "@types/react-native": "0.57.43",
        "@types/react-native-auth0": "1.3.0",
        "@types/react-native-vector-icons": "6.4.0",
        "@types/react-redux": "^7.1.1",
        "babel-core": "7.0.0-bridge.0",
        "babel-eslint": "10.0.1",
        "babel-jest": "24.3.1",
        "babel-preset-expo": "5.1.1",
        "enzyme": "^3.10.0",
        "enzyme-adapter-react-16": "^1.14.0",
        "enzyme-to-json": "^3.4.0",
        "eslint": "5.15.1",
        "eslint-config-airbnb": "17.1.0",
        "eslint-plugin-import": "2.16.0",
        "eslint-plugin-jsx-a11y": "6.2.1",
        "eslint-plugin-react": "7.12.4",
        "jest": "^24.3.1",
        "jest-fetch-mock": "^2.1.2",
        "jest-localstorage-mock": "^2.4.0",
        "metro-react-native-babel-preset": "0.53.0",
        "react-dom": "^16.8.6",
        "react-native-typescript-transformer": "1.2.12",
        "react-test-renderer": "^16.6.3",
        "redux-mock-store": "^1.5.3",
        "ts-jest": "^24.0.2",
        "tslint": "5.16.0",
        "typescript": "3.4.3"
      },
      "jest": {
        "preset": "react-native"
      }
    }

jest.config.js

module.exports = {
  "preset": 'react-native',
  "verbose": true,
  "setupFilesAfterEnv": ["<rootDir>/__tests__/setup.js", "jest-localstorage-mock"],
  "roots": [
    "<rootDir>/src"
  ],
  "transform": {
    "^.+\\.tsx?$": "ts-jest",
  },
  "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
  "moduleFileExtensions": [
    "ts",
    "tsx",
    "js",
    "jsx",
    "json",
    "node"
  ],
  "testPathIgnorePatterns": ['/node_modules/'],
  "snapshotSerializers": ["enzyme-to-json/serializer"],
  // "collectCoverageFrom": ["src/**/*.tsx"],
  "collectCoverage": true,
};

babel.config.js

module.exports = {
  "presets": ["module:metro-react-native-babel-preset"]
}
4

3 回答 3

1

我一直在努力解决同样的问题。

基于github上的这个评论来自harudev

[...]所以我测试了一些文件来测试项目,并发现“react-native.config.js”文件是原因。如果有人遇到此问题,请尝试从您的“*.config.js”之一中删除文件或选项

所以我确实删除了所有*.config.js文件,错误现在消失了。希望这可以帮助你。请注意,您可以使用以下命令重现*.confing.js文件的行为package.json

于 2019-09-06T11:37:07.030 回答
1

对我来说,这发生在 RN 从 0.59.10 到 0.61.4 的更新中。

我尝试了几个修复,但没有任何效果。

我发现的唯一解决方法是设置metro.config.js如下:

const blacklist = require('metro-config/src/defaults/blacklist');

module.exports = {
  resolver: {
    blacklistRE: blacklist([
      /node_modules\/.*\/node_modules\/react-native\/.*/,
    ]),
  },
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: false,
      },
    }),
  },
};

资料来源: jamalx31的回答来自这里:

https://github.com/facebook/react-native/issues/24065#issuecomment-537489786

该问题在此处报告和记录:

https://github.com/facebook/react-native/issues/24065

和这里:

https://github.com/facebook/react-native/issues/23943

于 2019-11-21T14:09:31.540 回答
0

有同样的错误,经过大量的灵魂搜索(谷歌搜索),我只是npx react-native start反对运行我的本地 react-native。

于 2022-03-01T06:35:58.240 回答