Joe
Joe

Reputation: 7004

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?

Upvotes: 4

Views: 6670

Answers (1)

Joe
Joe

Reputation: 7004

Ah. I'm an idiot. There's nothing wrong with jest, the test is correctly failing. I'm reading the output of Jest wrong.

It's saying the whole object:

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

Is missing from the array. Although it is also reporting the undefined as a difference, it's not the thing causing toEqual to fail. Notice the destination is different. The test should be returning two objects.

Upvotes: 3

Related Questions