CodePark/ questions / Unit testing services
START PRACTICINGPRACTICE
Backend question · Backend testing › Unit testing services

A service returns { id: 1, name: 'Ann', deletedAt: undefined }. Which assertion against { id: 1, name: 'Ann' } fails?

expect(result).toStrictEqual({ id: 1, name: 'Ann' })

"expect(result).toStrictEqual({ id: 1, name: 'Ann' })" fails. toStrictEqual treats a key with the value undefined as different from a missing key. Here deletedAt: undefined exists in the result but not in the expected object. toStrictEqual also checks that class types match.

"
expect(result).toEqual({ id: 1, name: 'Ann' })" passes. toEqual ignores properties with undefined values, at every level of the object.