【问题标题】:Skip checking a specific property in Jest snapshot跳过检查 Jest 快照中的特定属性
【发布时间】:2020-03-25 01:35:27
【问题描述】:

我需要测试一个快照,该快照还有一个名为aria-labelledby 的属性,并且由一些我无法修改或模拟的函数创建。每次我运行测试时,该属性都会获得一个新值,我也不关心它是什么。

那么,有没有办法提aria-labelledby不被检查?

测试

it('My Test', () => {
    let wrapper: ReactWrapper;
    wrapper = mount(<App />);
    expect(wrapper.find(App)).toMatchSnapshot();
});

快照

.....
    }
    unmountOnExit={false}
    >
    <div
-       aria-labelledby="pr_id_1_label"      <===== SNAPSHOT VALUE
+       aria-labelledby="pr_id_2_label"      <===== NEW VALUE WHICH CAUSES THE TEST FAIL!!!!
        className="p-dialog p-component"
.....

【问题讨论】:

  • 哪个库添加了aria-labelledby?我也假设在某个地方有&lt;label id= 也是动态的,对吧?
  • @skyboyer aria-labelledby 它来自 primereactdialog 组件。但是我在其他地方动态添加了例如key 属性,这也会导致同样的问题。

标签: javascript reactjs jestjs enzyme snapshot


【解决方案1】:

作为一种解决方法,使用enzyme-to-json 我们may specify .map() option 能够过滤道具:

const removeLabelledBy = 
  ({"aria-labelledby": unused, ...restProps}) => restProps;
expect(
  toJson(wrapper, {
    mode: "deep",
    map: removeLabelledBy
  })
).toMatchSnapshot();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    • 2018-11-04
    • 2017-12-17
    相关资源
    最近更新 更多