TypeError:_reactNativeLanguages.default.addEventListener不是Jest中的函数

时间:2019-02-08 05:30:13

标签: unit-testing react-native jestjs

我已经完成了我的第一个本机应用程序。现在,我想进行单元测试,并使用 jest 获得整个应用程序的代码覆盖率。我使用了 i18n-jsreact-native-languages 之类的npm软件包。

splash-test.js:

import React from 'react';
import Splash from '../Src/components/authentication/Splash';

import renderer from 'react-test-renderer';


test('renders correctly',()=>{
const tree = renderer.create(
<Splash />
).toJSON();
expect(tree).toMatchSnapshot();
})

env.js:

jest.mock('react-native-languages', () => ({
RNLanguages: {
language: 'en',
languages: ['en'],
},
}));

package.json:


{
  "name": "app-name",
  "version": "1.0.4",
  "private": true,
  "rnpm": {
    "assets": [
      "Src/assets/fonts/"
    ]
  },
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest --coverage --coverageDirectory=output/coverage/jest"
  },
  "dependencies": {
    "i18n-js": "^3.1.0",
    "react": "16.6.3",
    "react-native": "0.57.8",
    "react-native-fast-image-zoom-viewer": "^2.3.0",
    "react-native-firebase": "^5.1.1",
    "react-native-gifted-chat": "^0.7.2",
    "react-native-image-crop-picker": "^0.21.3",
    "react-native-image-zoom-viewer": "^2.2.25",
    "react-native-languages": "^3.0.1",
    "react-native-material-dropdown": "^0.11.1",
    "react-native-modal": "^7.0.2",
    "react-native-permissions": "^1.1.1",
    "react-native-progress": "^3.5.0",
    "react-native-restart": "0.0.9",
    "react-native-spinkit": "^1.1.1",
    "react-native-swiper": "^1.5.14",
    "react-native-view-toast": "0.0.1",
    "react-navigation": "^3.0.8",
    "react-redux": "^6.0.0",
    "realm": "^2.22.0",
    "redux": "^4.0.1",
    "redux-thunk": "^2.3.0",
    "rn-fetch-blob": "^0.10.15",
    "tipsi-stripe": "^7.1.0"
  },
  "devDependencies": {
    "babel-jest": "^23.6.0",
    "jest": "^23.6.0",
    "jest-junit": "^6.2.1",
    "metro-react-native-babel-preset": "^0.51.0",
    "react-test-renderer": "^16.6.3"
  },
  "jest": {
    "preset": "react-native",
    "transform": {
      "^.+\\.(js)$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
    },
    "setupFiles": [
      "./test/env.js"
    ],
    "coverageReporters": [
      "text"
    ],
    "reporters": [
      "default",
      "jest-junit"
    ],
    "collectCoverageFrom": [
      "Src/**/*.{js,jsx}",
      "!/node_modules/"
    ],
    "transformIgnorePatterns": [
      "node_modules/(?!(react-native|react-native-languages)/)"
    ]
  },
  "jest-junit": {
    "output": "output/coverage/junit/junit.xml",
    "usePathForSuiteName": "true"
  }
}

现在,如果我进行 npm测试,则会引发错误,如我在标题中所述。有人告诉我如何绕过我在应用程序中使用过的所有npm模块。

谢谢!

0 个答案:

没有答案