Skip to content

Commit

Permalink
Replace identity equality with Object.is in toBe matcher (#4917)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkubilayk authored and cpojer committed Nov 20, 2017
1 parent 27a4c2a commit 57efef0
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 28 deletions.
12 changes: 6 additions & 6 deletions docs/ExpectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ for the `toBe` matcher:
const diff = require('jest-diff');
expect.extend({
toBe(received, expected) {
const pass = received === expected;
const pass = Object.is(received, expected);

const message = pass
? () =>
this.utils.matcherHint('.not.toBe') +
'\n\n' +
`Expected value to not be (using ===):\n` +
`Expected value to not be (using Object.is):\n` +
` ${this.utils.printExpected(expected)}\n` +
`Received:\n` +
` ${this.utils.printReceived(received)}`
Expand All @@ -121,7 +121,7 @@ expect.extend({
return (
this.utils.matcherHint('.toBe') +
'\n\n' +
`Expected value to be (using ===):\n` +
`Expected value to be (using Object.is):\n` +
` ${this.utils.printExpected(expected)}\n` +
`Received:\n` +
` ${this.utils.printReceived(received)}` +
Expand All @@ -139,7 +139,7 @@ This will print something like this:
```
expect(received).toBe(expected)
Expected value to be (using ===):
Expected value to be (using Object.is):
"banana"
Received:
"apple"
Expand Down Expand Up @@ -430,8 +430,8 @@ test('rejects to octopus', async () => {

### `.toBe(value)`

`toBe` just checks that a value is what you expect. It uses `===` to check
strict equality.
`toBe` just checks that a value is what you expect. It uses `Object.is` to check
exact equality.

For example, this code will validate some properties of the `can` object:

Expand Down
2 changes: 1 addition & 1 deletion docs/UsingMatchers.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ won't do much with these expectation objects except call matchers on them. In
this code, `.toBe(4)` is the matcher. When Jest runs, it tracks all the failing
matchers so that it can print out nice error messages for you.

`toBe` uses `===` to test exact equality. If you want to check the value of an
`toBe` uses `Object.is` to test exact equality. If you want to check the value of an
object, use `toEqual` instead:

```js
Expand Down
47 changes: 30 additions & 17 deletions packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ Received: <red>undefined</>"
exports[`.toBe() does not crash on circular references 1`] = `
"<dim>expect(</><red>received</><dim>).toBe(</><green>expected</><dim>)</>

Expected value to be (using ===):
Expected value to be (using Object.is):
<green>{}</>
Received:
<red>{\\"circular\\": [Circular]}</>
Expand All @@ -160,7 +160,7 @@ Difference:
exports[`.toBe() fails for '"a"' with '.not' 1`] = `
"<dim>expect(</><red>received</><dim>).not.toBe(</><green>expected</><dim>)</>

Expected value to not be (using ===):
Expected value to not be (using Object.is):
<green>\\"a\\"</>
Received:
<red>\\"a\\"</>"
Expand All @@ -169,7 +169,7 @@ Received:
exports[`.toBe() fails for '[]' with '.not' 1`] = `
"<dim>expect(</><red>received</><dim>).not.toBe(</><green>expected</><dim>)</>

Expected value to not be (using ===):
Expected value to not be (using Object.is):
<green>[]</>
Received:
<red>[]</>"
Expand All @@ -178,7 +178,7 @@ Received:
exports[`.toBe() fails for '{}' with '.not' 1`] = `
"<dim>expect(</><red>received</><dim>).not.toBe(</><green>expected</><dim>)</>

Expected value to not be (using ===):
Expected value to not be (using Object.is):
<green>{}</>
Received:
<red>{}</>"
Expand All @@ -187,7 +187,7 @@ Received:
exports[`.toBe() fails for '1' with '.not' 1`] = `
"<dim>expect(</><red>received</><dim>).not.toBe(</><green>expected</><dim>)</>

Expected value to not be (using ===):
Expected value to not be (using Object.is):
<green>1</>
Received:
<red>1</>"
Expand All @@ -196,7 +196,7 @@ Received:
exports[`.toBe() fails for 'false' with '.not' 1`] = `
"<dim>expect(</><red>received</><dim>).not.toBe(</><green>expected</><dim>)</>

Expected value to not be (using ===):
Expected value to not be (using Object.is):
<green>false</>
Received:
<red>false</>"
Expand All @@ -205,7 +205,7 @@ Received:
exports[`.toBe() fails for 'null' with '.not' 1`] = `
"<dim>expect(</><red>received</><dim>).not.toBe(</><green>expected</><dim>)</>

Expected value to not be (using ===):
Expected value to not be (using Object.is):
<green>null</>
Received:
<red>null</>"
Expand All @@ -214,7 +214,7 @@ Received:
exports[`.toBe() fails for 'undefined' with '.not' 1`] = `
"<dim>expect(</><red>received</><dim>).not.toBe(</><green>expected</><dim>)</>

Expected value to not be (using ===):
Expected value to not be (using Object.is):
<green>undefined</>
Received:
<red>undefined</>"
Expand All @@ -223,7 +223,7 @@ Received:
exports[`.toBe() fails for: "abc" and "cde" 1`] = `
"<dim>expect(</><red>received</><dim>).toBe(</><green>expected</><dim>)</>

Expected value to be (using ===):
Expected value to be (using Object.is):
<green>\\"cde\\"</>
Received:
<red>\\"abc\\"</>"
Expand All @@ -233,7 +233,7 @@ exports[`.toBe() fails for: "with
trailing space" and "without trailing space" 1`] = `
"<dim>expect(</><red>received</><dim>).toBe(</><green>expected</><dim>)</>

Expected value to be (using ===):
Expected value to be (using Object.is):
<green>\\"without trailing space\\"</>
Received:
<red>\\"with<inverse> </></>
Expand All @@ -243,7 +243,7 @@ Received:
exports[`.toBe() fails for: [] and [] 1`] = `
"<dim>expect(</><red>received</><dim>).toBe(</><green>expected</><dim>)</>

Expected value to be (using ===):
Expected value to be (using Object.is):
<green>[]</>
Received:
<red>[]</>
Expand All @@ -256,7 +256,7 @@ Difference:
exports[`.toBe() fails for: {"a": 1} and {"a": 1} 1`] = `
"<dim>expect(</><red>received</><dim>).toBe(</><green>expected</><dim>)</>

Expected value to be (using ===):
Expected value to be (using Object.is):
<green>{\\"a\\": 1}</>
Received:
<red>{\\"a\\": 1}</>
Expand All @@ -269,7 +269,7 @@ Difference:
exports[`.toBe() fails for: {"a": 1} and {"a": 5} 1`] = `
"<dim>expect(</><red>received</><dim>).toBe(</><green>expected</><dim>)</>

Expected value to be (using ===):
Expected value to be (using Object.is):
<green>{\\"a\\": 5}</>
Received:
<red>{\\"a\\": 1}</>
Expand All @@ -288,7 +288,7 @@ Difference:
exports[`.toBe() fails for: {} and {} 1`] = `
"<dim>expect(</><red>received</><dim>).toBe(</><green>expected</><dim>)</>

Expected value to be (using ===):
Expected value to be (using Object.is):
<green>{}</>
Received:
<red>{}</>
Expand All @@ -298,10 +298,23 @@ Difference:
<dim>Compared values have no visual difference.</> <dim>Looks like you wanted to test for object/array equality with strict \`toBe\` matcher. You probably need to use \`toEqual\` instead.</>"
`;

exports[`.toBe() fails for: -0 and 0 1`] = `
"<dim>expect(</><red>received</><dim>).toBe(</><green>expected</><dim>)</>

Expected value to be (using Object.is):
<green>0</>
Received:
<red>-0</>

Difference:

<dim>Compared values have no visual difference.</>"
`;

exports[`.toBe() fails for: 1 and 2 1`] = `
"<dim>expect(</><red>received</><dim>).toBe(</><green>expected</><dim>)</>

Expected value to be (using ===):
Expected value to be (using Object.is):
<green>2</>
Received:
<red>1</>"
Expand All @@ -310,7 +323,7 @@ Received:
exports[`.toBe() fails for: null and undefined 1`] = `
"<dim>expect(</><red>received</><dim>).toBe(</><green>expected</><dim>)</>

Expected value to be (using ===):
Expected value to be (using Object.is):
<green>undefined</>
Received:
<red>null</>
Expand All @@ -323,7 +336,7 @@ Difference:
exports[`.toBe() fails for: true and false 1`] = `
"<dim>expect(</><red>received</><dim>).toBe(</><green>expected</><dim>)</>

Expected value to be (using ===):
Expected value to be (using Object.is):
<green>false</>
Received:
<red>true</>"
Expand Down
3 changes: 3 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ describe('.resolves', () => {
expect(error.message).toMatchSnapshot();
});
});

describe('.toBe()', () => {
it('does not throw', () => {
jestExpect('a').not.toBe('b');
Expand All @@ -114,6 +115,7 @@ describe('.toBe()', () => {
jestExpect(null).not.toBe(undefined);
jestExpect(null).toBe(null);
jestExpect(undefined).toBe(undefined);
jestExpect(NaN).toBe(NaN);
});

[
Expand All @@ -126,6 +128,7 @@ describe('.toBe()', () => {
['with \ntrailing space', 'without trailing space'],
[[], []],
[null, undefined],
[-0, +0],
].forEach(([a, b]) => {
it(`fails for: ${stringify(a)} and ${stringify(b)}`, () => {
expect(() => jestExpect(a).toBe(b)).toThrowErrorMatchingSnapshot();
Expand Down
6 changes: 3 additions & 3 deletions packages/expect/src/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ type ContainIterable =

const matchers: MatchersObject = {
toBe(received: any, expected: number) {
const pass = received === expected;
const pass = Object.is(received, expected);

const message = pass
? () =>
matcherHint('.not.toBe') +
'\n\n' +
`Expected value to not be (using ===):\n` +
`Expected value to not be (using Object.is):\n` +
` ${printExpected(expected)}\n` +
`Received:\n` +
` ${printReceived(received)}`
Expand All @@ -63,7 +63,7 @@ const matchers: MatchersObject = {
return (
matcherHint('.toBe') +
'\n\n' +
`Expected value to be (using ===):\n` +
`Expected value to be (using Object.is):\n` +
` ${printExpected(expected)}\n` +
`Received:\n` +
` ${printReceived(received)}` +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`matchers proxies matchers to expect 1`] = `
"<dim>expect(</><red>received</><dim>).toBe(</><green>expected</><dim>)</>
Expected value to be (using ===):
Expected value to be (using Object.is):
<green>2</>
Received:
<red>1</>"
Expand Down

0 comments on commit 57efef0

Please sign in to comment.