尝试使用jest和酶测试我的反应原生应用程序如下。
Cannot find module 'enzyme/build/ShallowTraversal' from 'shallow.js'
运行test('SignUp Page renders correctly', () => {
const tree = renderer.create(<SignUp />).toJSON();
expect(tree).toMatchSnapshot();
});
时我收到错误chmod 755 platforms/android/gradlew
测试套件无法运行。
请注意,测试完成减去酶运行
remove()
可能出现什么问题?
答案 0 :(得分:5)
首先安装enzyme-to-json
来解决问题; npm install --save-dev enzyme-to-json
在我的test_file.js
中导入toJson
import 'react-native';
import React from 'react';
import renderer from 'react-test-renderer';
import { shallow } from 'enzyme';
import toJson from 'enzyme-to-json'; //added this line
将测试更改为此
describe('Testing SignUp component', () => {
it('renders as expected', () => {
const wrapper = shallow(
<SignUp />
);
expect(toJson(wrapper)).toMatchSnapshot(); //edited this line
});
});
更改了package.json中的snapShotSerializers
"jest": {
"preset": "react-native",
"collectCoverage": true,
"collectCoverageFrom": [
"**/src/**.{js,jsx}"
],
"snapshotSerializers": ["enzyme-to-json/serializer"] //added this line
现在我的测试运行正常。