首页 > 解决方案 > Jest/expect "toEquals" failing with undefined properties

问题描述

I'm using expect(item).toEqual(otherItem). Acording to everything I've read, expect({ a: "a", b: undefined }).toEqual({ a: "a" }) should pass. Otherwise I should be using toStrictEqual. E.g. here, in "basic matches".

However, I'm getting a failure with the output:

     Array [
       Object {
   +     "alertId": undefined,
         "attributes": Array [],
         "destination": "_b",
         "kind": "kind",
         "linkable": "Unlinkable",
         "source": "_a",
   +     "validFrom": undefined,
   +     "validTo": undefined,
       },
   -   Object {
   -     "attributes": Array [],
   -     "destination": "_c",
   -     "kind": "kind",
   -     "linkable": "Unlinkable",
   -     "source": "_a",
   -   },
     ]

Any idea why?

标签: javascriptjestjs

解决方案


啊。我是个白痴。开玩笑没有错,测试正确地失败了。我读错了 Jest 的输出。

这是说整个对象:

-   Object {
-     "attributes": Array [],
-     "destination": "_c",
-     "kind": "kind",
-     "linkable": "Unlinkable",
-     "source": "_a",

从数组中丢失。尽管它也报告undefined了差异,但这并不是导致toEqual失败的原因。注意destination是不同的。测试应该返回两个对象。


推荐阅读