enzyme-to-snapshot render object as JSON

左心房为你撑大大i 提交于 2019-12-13 17:23:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!