Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce toStrictEqual #6032

Merged
merged 6 commits into from
Apr 19, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,32 @@ describe('.toBe()', () => {
});
});

describe('.toStrictEqual()', () => {
class TestClass {
constructor(a, b) {
this.a = a;
this.b = b;
}
}

expect({
test: new TestClass(1, 2),
}).toStrictEqual({test: new TestClass(1, 2)});
});

describe('.not.toStrictEqual()', () => {
class TestClass {
constructor(a, b) {
this.a = a;
this.b = b;
}
}

expect({
test: new TestClass(1, 2),
}).not.toStrictEqual({test: {a: 1, b: 2}});
});

describe('.toEqual()', () => {
[
[true, false],
Expand Down
33 changes: 33 additions & 0 deletions packages/expect/src/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
getPath,
iterableEquality,
subsetEquality,
typeEquality,
} from './utils';
import {equals} from './jasmine_utils';

Expand Down Expand Up @@ -615,6 +616,38 @@ const matchers: MatchersObject = {

return {message, pass};
},

toStrictEqual(received: any, expected: any) {
const pass = equals(received, expected, [iterableEquality, typeEquality]);

const message = pass
? () =>
matcherHint('.not.toStrictEqual') +
'\n\n' +
`Expected value to not equal:\n` +
` ${printExpected(expected)}\n` +
`Received:\n` +
` ${printReceived(received)}`
: () => {
const diffString = diff(expected, received, {
expand: this.expand,
});
return (
matcherHint('.toStrictEqual') +
'\n\n' +
`Expected value to equal:\n` +
` ${printExpected(expected)}\n` +
`Received:\n` +
` ${printReceived(received)}` +
(diffString ? `\n\nDifference:\n\n${diffString}` : '')
);
};

// Passing the the actual and expected objects so that a custom reporter
// could access them, for example in order to display a custom visual diff,
// or create a different error message
return {actual: received, expected, message, name: 'toStrictEqual', pass};
},
};

export default matchers;
8 changes: 8 additions & 0 deletions packages/expect/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ 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) {
return undefined;
}

return false;
};

export const partition = <T>(
items: Array<T>,
predicate: T => boolean,
Expand Down