Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jest时获取错误:“jest.mock()”的模块工厂不允许引用任何范围外变量_Javascript_Unit Testing_React Native_Jestjs - Fatal编程技术网

Javascript jest时获取错误:“jest.mock()”的模块工厂不允许引用任何范围外变量

Javascript jest时获取错误:“jest.mock()”的模块工厂不允许引用任何范围外变量,javascript,unit-testing,react-native,jestjs,Javascript,Unit Testing,React Native,Jestjs,尝试运行纱线测试并尝试了多个版本的node(8和11),react(57,58),但无法修复此错误 FAIL __tests__/index.ios.js ● Test suite failed to run /xyz/mobile-react/node_modules/react-native/jest/setup.js: babel-plugin-jest-hoist: The module factory of `jest.mock()` is not allowed t

尝试运行
纱线测试
并尝试了多个版本的node(8和11),react(57,58),但无法修复此错误

 FAIL  __tests__/index.ios.js
  ● Test suite failed to run

    /xyz/mobile-react/node_modules/react-native/jest/setup.js: babel-plugin-jest-hoist: The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables.
    Invalid variable access: MockNativeMethods
    Whitelisted objects: Array, ArrayBuffer, Boolean, DataView, Date, Error, EvalError, Float32Array, Float64Array, Function, Generator, GeneratorFunction, Infinity, Int16Array, Int32Array, Int8Array, InternalError, Intl, JSON, Map, Math, NaN, Number, Object, Promise, Proxy, RangeError, ReferenceError, Reflect, RegExp, Set, String, Symbol, SyntaxError, TypeError, URIError, Uint16Array, Uint32Array, Uint8Array, Uint8ClampedArray, WeakMap, WeakSet, arguments, expect, jest, require, undefined, console, DTRACE_NET_SERVER_CONNECTION, DTRACE_NET_STREAM_END, DTRACE_HTTP_SERVER_REQUEST, DTRACE_HTTP_SERVER_RESPONSE, DTRACE_HTTP_CLIENT_REQUEST, DTRACE_HTTP_CLIENT_RESPONSE, global, process, Buffer, clearImmediate, clearInterval, clearTimeout, setImmediate, setInterval, setTimeout.
    Note: This is a precaution to guard against uninitialized mock variables. If it is ensured that the mock is required lazily, variable names prefixed with `mock` are permitted.

      at invariant (node_modules/babel-jest/node_modules/babel-plugin-jest-hoist/build/index.js:13:11)
package.json:

{
  "name": "XYZ",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "android-local": "ENVFILE=.env.local react-native run-android",
    "android-dev": "ENVFILE=.env.dev react-native run-android",
    "android-qa": "ENVFILE=.env.qa react-native run-android",
    "build-android-dev": "export ENVFILE=.env.dev && cd android && ./gradlew assembleRelease && cd ..",
    "build-android-qa": "export ENVFILE=.env.qa && cd android && ./gradlew assembleRelease && cd ..",
    "test": "jest"
  },
  "rnpm": {
    "assets": [
      "./assets/fonts/"
    ]
  },
  "dependencies": {
    "chai": "^4.1.2",
    "chai-http": "^4.0.0",
    "eslint-config-rallycoding": "^3.2.0",
    "fs": "0.0.1-security",
    "google-auth-library": "^0.11.0",
    "googleapis": "^22.2.0",
    "jetifier": "^1.6.4",
    "lodash": "^4.17.4",
    "moment": "^2.18.1",
    "node-fetch": "^2.1.2",
    "prop-types": "^15.5.10",
    "q": "^1.5.0",
    "react": "16.6.1",
    "react-native": "0.58",
    "react-native-android-location-enabler": "^1.0.5",
    "react-native-android-location-services-dialog-box": "^2.0.0",
    "react-native-config": "^0.6.1",
    "react-native-datepicker": "^1.6.0",
    "react-native-hockeyapp": "^0.5.3",
    "react-native-image-crop-picker": "^0.16.0",
    "react-native-image-resizer": "1.0.0",
    "react-native-localization": "^0.2.1",
    "react-native-maps": "^0.21.0",
    "react-native-tab-view": "0.0.75",
    "react-native-vector-icons": "6.1.0",
    "react-native-version-number": "^0.1.2",
    "react-navigation": "^1.0.0-beta.11",
    "react-navigation-redux-helpers": "^1.0.3",
    "react-redux": "^5.0.5",
    "readline": "^1.3.0",
    "redux": "^3.7.2",
    "redux-persist": "^5.9.1",
    "redux-thunk": "^2.2.0"
  },
  "devDependencies": {
    "babel-jest": "20.0.3",
    "babel-plugin-transform-object-rest-spread": "^7.0.0-beta.3",
    "babel-preset-jest": "22",
    "babel-preset-react-native": "4.0.1",
    "jest": "20.0.4",
    "mocha": "^5.1.1",
    "react-test-renderer": "16.0.0-alpha.12"
  },
  "jest": {
    "preset": "react-native"
  }
}
B.法律改革委员会

{
  "presets": ["react-native"]
}

如果看不到确切的代码,很难说问题出在哪里。但是,关键在注释中:

注意:这是防止未初始化的模拟变量的预防措施。如果确保延迟需要mock,则允许使用前缀为
mock
的变量名

问题是,笑话模拟会被提升到页面顶部。因此,您不能在模拟中引用变量。。。因为变量还不存在。见:

要克服这一点,您需要按照注释中的说明,在变量前面加上单词“mock”

下面是一个示例,其中需要覆盖导入
getAppPages
,以便它返回一个伪数组(
appPages


如果看不到确切的代码,很难说问题出在哪里。但是,关键在注释中:

注意:这是防止未初始化的模拟变量的预防措施。如果确保延迟需要mock,则允许使用前缀为
mock
的变量名

问题是,笑话模拟会被提升到页面顶部。因此,您不能在模拟中引用变量。。。因为变量还不存在。见:

要克服这一点,您需要按照注释中的说明,在变量前面加上单词“mock”

下面是一个示例,其中需要覆盖导入
getAppPages
,以便它返回一个伪数组(
appPages

import React from 'react';

import { getAppPages } from 'utils';
import App from './App';
import { fireEvent, render } from '../../../test/test-utils';
import appPages from '../../../__mocks__/data/appPages';

const mockAppPages = appPages;

jest.mock('utils', () => ({
  getAppPages: () => mockAppPages,
}));

describe('App', () => {
  test('renders and defaults to home page and matches snapshot', () => {
    ...
  });
});