【问题标题】:enzyme-to-snapshot render object as JSON酶到快照将对象渲染为 JSON
【发布时间】:2017-06-07 01:07:38
【问题描述】:

为什么要在快照中渲染对象 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",。所以我的快照总是失败。

【问题讨论】:

  • 我认为安装应该是这样的:const wrapper = mount(&lt;MyComp /&gt; )

标签: reactjs jestjs enzyme enzyme-to-snapshot


【解决方案1】:

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

toJson(wrapper, {
  noKey: true
});

【讨论】:

    猜你喜欢
    • 2014-01-07
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    • 2022-09-25
    相关资源
    最近更新 更多