From 6252af6539ab957b0ad4ca69465cf9514dd21316 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olle=20Lauri=20Bostr=C3=B6m?= Date: Wed, 19 Sep 2018 23:19:15 +0200 Subject: [PATCH] Check constructor equality in .toStrictEqual() --- CHANGELOG.md | 1 + .../expect/src/__tests__/matchers.test.js | 36 ++++++++++++++++--- packages/expect/src/utils.js | 2 +- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1020684b838e..8dbfbfad66f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - `[jest-worker]` [**BREAKING**] Add functionality to call a `setup` method in the worker before the first call and a `teardown` method when ending the farm ([#7014](https://github.com/facebook/jest/pull/7014)). - `[jest-config]` [**BREAKING**] Set default `notifyMode` to `failure-change` ([#7024](https://github.com/facebook/jest/pull/7024)) - `[jest-snapshot]` Enable configurable snapshot paths ([#6143](https://github.com/facebook/jest/pull/6143)) +- `[expect]` Check constructor equality in .toStrictEqual() ([#7005](https://github.com/facebook/jest/pull/7005)) ### Fixes diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index 36132f537bb1..e695eec418ed 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -205,13 +205,32 @@ describe('.toBe()', () => { }); describe('.toStrictEqual()', () => { - class TestClass { + class TestClassA { constructor(a, b) { this.a = a; this.b = b; } } + class TestClassB { + constructor(a, b) { + this.a = a; + this.b = b; + } + } + + const TestClassC = class Child extends TestClassA { + constructor(a, b) { + super(a, b); + } + }; + + const TestClassD = class Child extends TestClassB { + constructor(a, b) { + super(a, b); + } + }; + it('does not ignore keys with undefined values', () => { expect({ a: undefined, @@ -221,14 +240,21 @@ describe('.toStrictEqual()', () => { it('passes when comparing same type', () => { expect({ - test: new TestClass(1, 2), - }).toStrictEqual({test: new TestClass(1, 2)}); + test: new TestClassA(1, 2), + }).toStrictEqual({test: new TestClassA(1, 2)}); }); it('does not pass for different types', () => { expect({ - test: new TestClass(1, 2), - }).not.toStrictEqual({test: {a: 1, b: 2}}); + test: new TestClassA(1, 2), + }).not.toStrictEqual({test: new TestClassB(1, 2)}); + }); + + it('does not simply compare constructor names', () => { + const c = new TestClassC(1, 2); + const d = new TestClassD(1, 2); + expect(c.constructor.name).toEqual(d.constructor.name); + expect({test: c}).not.toStrictEqual({test: d}); }); }); diff --git a/packages/expect/src/utils.js b/packages/expect/src/utils.js index c25e9cc46c5d..9a7a0220fac5 100644 --- a/packages/expect/src/utils.js +++ b/packages/expect/src/utils.js @@ -213,7 +213,7 @@ export const subsetEquality = (object: Object, subset: Object) => { }; export const typeEquality = (a: any, b: any) => { - if (a == null || b == null || a.constructor.name === b.constructor.name) { + if (a == null || b == null || a.constructor === b.constructor) { return undefined; }