React Native Jest错误:无法从“ react-native-implementation.js”中找到模块“ warnOnce”

时间:2019-08-12 01:32:30

标签: react-native jestjs babel-jest ts-jest

运行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();
  })
})

有想法吗?

package.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"]
}

2 个答案:

答案 0 :(得分:1)

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

基于对github来自harudev的评论

  

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

因此,我确实删除了所有*.config.js文件,该错误现在消失了。 希望这可以对您有所帮助。 请注意,您可以使用*.confing.js

重现package.json文件的行为。

答案 1 :(得分:0)

对我来说,这发生在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