我试图用酶和开玩笑为反应原生成分写一个基本测试。我得到一个错误,暗指酶未正确导入:
● SearchPage › it starts spinner when page is loading
- ReferenceError: _enzyme is not defined
at Spec.eval (__tests__/basic-test.js:12:16)
1 test failed, 0 tests passed (1 total in 1 test suite, run time 1.113s)
npm ERR! Test failed. See above for more details.
我尝试重新安装酶包并盲目地摆弄代码,因为我是新手。基本测试不使用来自酶工作的浅层,即期望(真实).toBe(真实)。我已经搜索了很多关于JS中的referenceErrors的问题,但是在这种情况下似乎没有一个问题适用(无论如何我都没有经验)。
这是测试文件。非常裸露,限制了错误的来源。
jest.dontMock('../SearchPage');
import React from 'react';
import { shallow } from 'enzyme';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils';
const SearchPage = require('../SearchPage');
describe('SearchPage', () => {
it('starts spinner when page is loading', () => {
const wrapper = shallow(<SearchPage />);
// Check that it is false initially i.e. not loading.
expect(wrapper.state('isLoading')).toBe(false);
// Simulate a touch on 'Go' button and verify loading is now true
});
});
据我所知,我的package.json具有正确的依赖关系。我尝试重新安装酶但没有成功。这是:
{
"name": "PropertyFinder",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"react-native": "^0.20.0",
"react-dom": "~0.14.7"
},
"devDependencies": {
"babel-jest": "*",
"enzyme": "^2.0.0",
"jest-cli": "^0.8.2",
"react": "^0.14.7",
"react-addons-test-utils": "~0.14.0",
"react-native-mock": "0.0.6"
},
"jest": {
"scriptPreprocessor": "node_modules/react-native/jestSupport/preprocessor.js",
"setupEnvScriptFile": "node_modules/react-native/jestSupport/env.js",
"testPathIgnorePatterns": [
"/node_modules/",
"packager/react-packager/src/Activity/"
],
"testFileExtensions": [
"js"
],
"unmockedModulePathPatterns": [
"promise",
"source-map",
"react",
"react-dom",
"react-addons-test-utils",
"fbjs",
"enzyme",
"cheerio",
"htmlparser2",
"lodash",
"domhandler",
"object.assign",
"define-properties",
"function-bind",
"object-keys",
"object.values",
"es-abstract"
]
}
}
在此上下文中可能/将会抛出此ReferenceError?如果我尝试使用mocha / chai来查看是否有效,我可能很快就会发表评论,但此时我还是会开玩笑。
答案 0 :(得分:0)
这个问题来自于ES6进口的升级explained in more detail here。使用babel-jest作为预处理器将解决此问题,但保持&#34; setupEnvScriptFile&#34;。
非常重要。为我工作的例子
"jest": {
"preprocessorIgnorePatterns": [
"node_modules/enzyme"
],
"scriptPreprocessor": "<rootDir>/node_modules/babel-jest",
"setupEnvScriptFile": "node_modules/react-native/jestSupport/env.js",
"testPathIgnorePatterns": [
"/node_modules/",
"packager/react-packager/src/Activity/"
],
"unmockedModulePathPatterns": [
"react",
"react-dom",
"react-native",
"react-addons-test-utils",
"promise",
"source-map",
"enzyme"
]
},