我正在尝试使用酶和jest为反应原生组件编写基本测试 . 我得到一个错误,暗指酶未正确导入:
● 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来查看是否有效,我可能会很快发表评论,但此时我宁愿保持开玩笑 .
1 回答
这个问题来自ES6的进口升级explained in more detail here . 使用babel-jest作为预处理器将解决此问题,但保持"setupEnvScriptFile"非常重要 .
什么对我有用的例子