问题
Why render object JSON in snapshot?
For an component
const MyComp =
<Provider {...stores}>
<Router history={history}>
<ClassRoom.wrappedComponent {...props} />
</Router>
</Provider>,
I test as
const wrapper = mount(MyComp );
expect(toJson(wrapper)).toMatchSnapshot();
And my snapshot is generated with object json information.
<Provider
classRoomStore={ClassRoomStore {}}
mqttStore={
<MemoryRouter>
<Router
history={
Object {
"action": "POP",
"block": [Function],
"canGo": [Function],
"createHref": [Function],
"entries": Array [
Object {
"hash": "",
"key": "o0ynpg",
"pathname": "/",
"search": "",
"state": undefined,
},
],
With cause severals problems such as key": "o0ynpg",
is generated for every test. So my snapshot always fail.
回答1:
For anyone reading this post wondering about turning off keys in enzyme-to-json
in your snapshots:
toJson(wrapper, {
noKey: true
});
来源:https://stackoverflow.com/questions/44401936/enzyme-to-snapshot-render-object-as-json