.toStrictEqual(value)

Use .toStrictEqual to test that objects have the same types as well as structure.

Differences from .toEqual:

  • Keys with undefined properties are checked. e.g. {a: undefined, b: 2} does not match {b: 2} when using .toStrictEqual.
  • Array sparseness is checked. e.g. [, 1] does not match [undefined, 1] when using .toStrictEqual.
  • Object types are checked to be equal. e.g. A class instance with fields a and b will not equal a literal object with fields a and b.
  1. class LaCroix {
  2. constructor(flavor) {
  3. this.flavor = flavor;
  4. }
  5. }
  6. describe('the La Croix cans on my desk', () => {
  7. test('are not semantically the same', () => {
  8. expect(new LaCroix('lemon')).toEqual({flavor: 'lemon'});
  9. expect(new LaCroix('lemon')).not.toStrictEqual({flavor: 'lemon'});
  10. });
  11. });