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 20, 2018
1 parent ee751bf commit 4e0a130
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- `[pretty-format]` Option to not escape strings in diff messages ([#5661](https://github.com/facebook/jest/pull/5661))
- `[jest-haste-map]` Add `getFileIterator` to `HasteFS` for faster file iteration ([#7010](https://github.com/facebook/jest/pull/7010)).
- `[expect]` Check constructor equality in .toStrictEqual() ([#7005](https://github.com/facebook/jest/pull/7005))

### Fixes

Expand Down
35 changes: 30 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,20 @@ 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', () => {
expect({
test: new TestClassC(1, 2),
}).not.toStrictEqual({test: new TestClassD(1, 2)});
});
});

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 4e0a130

Please sign in to comment.