酶到快照渲染对象为 JSON

IT技术 reactjs jestjs enzyme enzyme-to-snapshot
2021-05-09 08:28:49

为什么要在快照中渲染对象 JSON?

对于一个组件

const MyComp =
  <Provider {...stores}>
    <Router history={history}>
      <ClassRoom.wrappedComponent {...props} />
    </Router>
  </Provider>,

我测试为

const wrapper = mount(MyComp );
expect(toJson(wrapper)).toMatchSnapshot();

我的快照是用对象 json 信息生成的。

<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,
                },
              ],

会导致一些问题,例如key": "o0ynpg",每次测试都会产生。所以我的快照总是失败。

1个回答

对于阅读这篇文章的任何想关闭enzyme-to-json快照中的键的人

toJson(wrapper, {
  noKey: true
});