Skip to content

Commit

Permalink
Check constructor equality in .toStrictEqual()
Browse files Browse the repository at this point in the history
  • Loading branch information
ollelauribostrom committed Sep 28, 2018
1 parent 728ca11 commit 6252af6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
36 changes: 31 additions & 5 deletions packages/expect/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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});
});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/expect/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 6252af6

Please sign in to comment.