我写了以下测试:
it('Can decrement the current step', function () {
expect(reducer(TestState, { type: 'GOTO_PREVIOUS_STEP' })).toMatchObject({ currentStep: 4 });
});
it('Can decrement the current step v2', function () {
expect(reducer(TestState, { type: 'GOTO_PREVIOUS_STEP' })).toEqual(expect.objectContaining({ currentStep: 4 }));
});
两个好像都过关了,有什么区别吗?它们之间有性能影响吗?
原文由 Julito Sanchis 发布,翻译遵循 CC BY-SA 4.0 许可协议
通过查看文档和我自己的实验来证实这一点,不同之处在于处理嵌套在作为期望传递的道具中的对象。
如果期望对象有一个属性,包含一个对象,该对象包含实际对象的等效属性中的 部分但不是全部 属性,则:
.toMatchObject()
仍然会通过, 如文档中所示。expect.objectContaining()
将失败(除非您在期望对象本身中使用expect.objectContaining()
声明该属性)示例(在 Jest 中测试):