Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
585 views
in Technique[技术] by (71.8m points)

javascript - 跳过检查Jest快照中的特定属性(Skip checking a specific property in Jest snapshot)

I need to test a snapshot which has also a property called aria-labelledby and created by some function that I cannot modify or mock.

(我需要测试一个快照,该快照也具有一个称为aria-labelledby的属性,并且由无法修改或模拟的某些函数创建。)

Everytime when I run the test, that property gets a new value which also I don't care what it is.

(每当我运行测试时,该属性都会获得一个新值,我也不在乎它是什么。)

So, is there a way to mention the aria-labelledby to be not checked?

(因此,有没有办法提及不被检查的aria-labelledby ?)

Test

(测试)

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

Snapshot

(快照)

.....
    }
    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"
.....
  ask by Korki Korkig translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As a workaround using enzyme-to-json we may specify .map() option to be able filtering props:

(作为使用enzyme-to-json的解决方法enzyme-to-json我们可以指定.map()选项以能够过滤道具:)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...