简体   繁体   English

React Native-Jest.mock必须是一个内联函数。 使用“ withNavigation”的故障测试组件

[英]React Native - Jest.mock must be an inline function. Trouble testing component that uses 'withNavigation'

I'm using Jest and Enzyme in my React Native app to test my component and I keep getting this error when testing babel-plugin-jest-hoist: The second argument of 'jest.mock' must be an inline function. 我在React Native应用程序中使用Jest和Enzyme来测试我的组件,并且在测试babel-plugin-jest-hoist: The second argument of 'jest.mock' must be an inline function.时不断出现此错误babel-plugin-jest-hoist: The second argument of 'jest.mock' must be an inline function. I don't really understand what I'm doing wrong because I am passing an inline function. 我不太了解自己在做什么错,因为我正在传递内联函数。

Here is my code in my test file (search-screen.tests.js): 这是我的测试文件(search-screen.tests.js)中的代码:

// All necessary imports here

jest.mock('react-navigation', ({ withNavigation: (component) => component}));

describe("Search screen renders appropriately and function work", () => {
    it("renders correctly", () => {
        const searchScreen = renderer.create(<SearchScreen />).toJSON();
        expect(searchScreen).toMatchSnapshot();
    });
});

I used this stack overflow post for reference 我将此堆栈溢出帖子用作参考

The main reason I'm trying to mock react-navigation is because my search screen component is exported using 'withNavigation' (like so: export default withNavigation(SearchScreen) ) and it breaks a lot of tests if I don't attempt to do this, is this the right thing to do? 我尝试模拟react-navigation主要原因是因为我的搜索屏幕组件是使用'withNavigation'导出的(例如: export default withNavigation(SearchScreen) ),如果我不尝试这样做,它将破坏很多测试这是正确的事吗?

Here is my package.json file as well, just incase. 这也是我的package.json文件,以防万一。

{
  "name": "app",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "ios-build": "sh scripts/ios/build-ipa.sh",
    "test": "jest"
  },
  "dependencies": {
    "axios": "0.18.0",
    "prop-types": "15.6.2",
    "react": "16.4.1",
    "react-native": "0.56.0",
    "react-native-cookies": "3.3.0",
    "react-native-elements": "0.19.1",
    "react-native-google-analytics-bridge": "6.0.1",
    "react-native-maps": "0.21.0",
    "react-native-snackbar": "0.5.1",
    "react-native-vector-icons": "4.6.0",
    "react-native-version-number": "0.3.4",
    "react-navigation": "2.9.1",
    "rxjs": "6.2.2"
  },
  "devDependencies": {
    "babel-jest": "23.4.0",
    "babel-preset-react-native": "5.0.2",
    "@babel/core": "^7.0.0-beta.47",
    "@babel/preset-env": "^7.0.0-beta.47",
    "@babel/preset-flow": "^7.0.0-beta.47",
    "babel-core": "^7.0.0-beta.47",
    "babel-eslint": "^8.2.5",
    "babel-loader": "^7.1.5",
    "babel-runtime": "^6.26.0",
    "enzyme": "^3.7.0",
    "enzyme-adapter-react-16": "^1.6.0",
    "jest": "23.4.0",
    "react-dom": "^16.6.0",
    "react-test-renderer": "16.4.1"
  },
  "jest": {
    "preset": "react-native",
    "transformIgnorePatterns": [
      "node_modules/(?!react-native|native-base|react-navigation)"
    ],
    "setupTestFrameworkScriptFile": "./setupTests.js",
    "setupFiles": [
      "./setupTests.js"
    ]
  }
}

So what can I do to stop getting the react-navigation error with Jest? 那么,我该怎么做才能停止收到Jest的反应导航错误? Let me know if you need more information from me. 让我知道您是否需要我的更多信息。 Any ideas will be helpful. 任何想法都会有所帮助。

I'm guessing that the problem is that, for all intents and purposes, you're passing an object as the second parameter to jest.mock. 我猜测问题在于,出于所有意图和目的,您正在将对象作为第二个参数传递给jest.mock。 The arrow function is within the object in the withNavigation key. 箭头功能位于withNavigation键中的对象内。

I believe that you need to pass it an arrow function that returns that same object, like this: 我相信您需要向它传递一个返回该对象的箭头函数,如下所示:

jest.mock('react-navigation', () => ({ withNavigation: (component) => component}));

I hope this helps! 我希望这有帮助! Cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM