对App.js进行更改时,我的测试不会失败。
我正在使用玩笑,酶,react.js,react-create-app和react-scripts。
在更新App.js组件时,我期望测试会失败,因为它与App.js的快照不匹配。我认为这与我的_快照_ / App.test.js文件有关...如何解决此问题?有什么想法吗?
这是我的代码:
src / components / App.js:
import React, { Component } from 'react';
class App extends Component {
render() {
return (
<div>
<h2>Gift Giver</h2>
</div>
);
}
}
export default App;
src / components / App.test.js:
import React from 'react';
import { shallow } from 'enzyme';
import App from './App';
const app = shallow(<App />);
it('renders correctly', () => {
expect(app).toMatchSnapshot();
});
src / components / __快照__ / App.test.js.snap
exports[`renders correctly 1`] = `ShallowWrapper {}`;
package.json:
{
"name": "giftgiver",
"version": "0.1.0",
"private": true,
"dependencies": {
"enzyme": "^3.10.0",
"react": "^16.8.6",
"react-bootstrap": "^1.0.0-beta.9",
"react-dom": "^16.8.6",
"react-scripts": "3.0.1",
"react-test-render": "^1.1.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"enzyme-adapter-react-16": "^1.14.0"
}
}
在更新App.js组件时,我期望测试会失败,因为它与App.js的快照不匹配
答案 0 :(得分:0)
渲染快照时必须使用.debug()
。
import React from 'react';
import { shallow } from 'enzyme';
import App from './App';
const app = shallow(<App />);
it('renders correctly', () => {
expect(app.debug()).to.matchSnapshot();
});
顺便说一句,通常最好使用mount
而不是shallow
,除非有很好的理由不这样做。