diff --git a/docs/ExpectAPI.md b/docs/ExpectAPI.md index 850681244c41..6df01bff12fa 100644 --- a/docs/ExpectAPI.md +++ b/docs/ExpectAPI.md @@ -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)}` @@ -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)}` + @@ -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" @@ -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: diff --git a/docs/UsingMatchers.md b/docs/UsingMatchers.md index e1362e50591e..902218736e6b 100644 --- a/docs/UsingMatchers.md +++ b/docs/UsingMatchers.md @@ -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 diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index c98f2be7cb3a..8b14ee7cf157 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -141,7 +141,7 @@ Received: undefined" exports[`.toBe() does not crash on circular references 1`] = ` "expect(received).toBe(expected) -Expected value to be (using ===): +Expected value to be (using Object.is): {} Received: {\\"circular\\": [Circular]} @@ -160,7 +160,7 @@ Difference: exports[`.toBe() fails for '"a"' with '.not' 1`] = ` "expect(received).not.toBe(expected) -Expected value to not be (using ===): +Expected value to not be (using Object.is): \\"a\\" Received: \\"a\\"" @@ -169,7 +169,7 @@ Received: exports[`.toBe() fails for '[]' with '.not' 1`] = ` "expect(received).not.toBe(expected) -Expected value to not be (using ===): +Expected value to not be (using Object.is): [] Received: []" @@ -178,7 +178,7 @@ Received: exports[`.toBe() fails for '{}' with '.not' 1`] = ` "expect(received).not.toBe(expected) -Expected value to not be (using ===): +Expected value to not be (using Object.is): {} Received: {}" @@ -187,7 +187,7 @@ Received: exports[`.toBe() fails for '1' with '.not' 1`] = ` "expect(received).not.toBe(expected) -Expected value to not be (using ===): +Expected value to not be (using Object.is): 1 Received: 1" @@ -196,7 +196,7 @@ Received: exports[`.toBe() fails for 'false' with '.not' 1`] = ` "expect(received).not.toBe(expected) -Expected value to not be (using ===): +Expected value to not be (using Object.is): false Received: false" @@ -205,7 +205,7 @@ Received: exports[`.toBe() fails for 'null' with '.not' 1`] = ` "expect(received).not.toBe(expected) -Expected value to not be (using ===): +Expected value to not be (using Object.is): null Received: null" @@ -214,7 +214,7 @@ Received: exports[`.toBe() fails for 'undefined' with '.not' 1`] = ` "expect(received).not.toBe(expected) -Expected value to not be (using ===): +Expected value to not be (using Object.is): undefined Received: undefined" @@ -223,7 +223,7 @@ Received: exports[`.toBe() fails for: "abc" and "cde" 1`] = ` "expect(received).toBe(expected) -Expected value to be (using ===): +Expected value to be (using Object.is): \\"cde\\" Received: \\"abc\\"" @@ -233,7 +233,7 @@ exports[`.toBe() fails for: "with trailing space" and "without trailing space" 1`] = ` "expect(received).toBe(expected) -Expected value to be (using ===): +Expected value to be (using Object.is): \\"without trailing space\\" Received: \\"with @@ -243,7 +243,7 @@ Received: exports[`.toBe() fails for: [] and [] 1`] = ` "expect(received).toBe(expected) -Expected value to be (using ===): +Expected value to be (using Object.is): [] Received: [] @@ -256,7 +256,7 @@ Difference: exports[`.toBe() fails for: {"a": 1} and {"a": 1} 1`] = ` "expect(received).toBe(expected) -Expected value to be (using ===): +Expected value to be (using Object.is): {\\"a\\": 1} Received: {\\"a\\": 1} @@ -269,7 +269,7 @@ Difference: exports[`.toBe() fails for: {"a": 1} and {"a": 5} 1`] = ` "expect(received).toBe(expected) -Expected value to be (using ===): +Expected value to be (using Object.is): {\\"a\\": 5} Received: {\\"a\\": 1} @@ -288,7 +288,7 @@ Difference: exports[`.toBe() fails for: {} and {} 1`] = ` "expect(received).toBe(expected) -Expected value to be (using ===): +Expected value to be (using Object.is): {} Received: {} @@ -298,10 +298,23 @@ Difference: Compared values have no visual difference. 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`] = ` +"expect(received).toBe(expected) + +Expected value to be (using Object.is): + 0 +Received: + -0 + +Difference: + +Compared values have no visual difference." +`; + exports[`.toBe() fails for: 1 and 2 1`] = ` "expect(received).toBe(expected) -Expected value to be (using ===): +Expected value to be (using Object.is): 2 Received: 1" @@ -310,7 +323,7 @@ Received: exports[`.toBe() fails for: null and undefined 1`] = ` "expect(received).toBe(expected) -Expected value to be (using ===): +Expected value to be (using Object.is): undefined Received: null @@ -323,7 +336,7 @@ Difference: exports[`.toBe() fails for: true and false 1`] = ` "expect(received).toBe(expected) -Expected value to be (using ===): +Expected value to be (using Object.is): false Received: true" diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index c0f368dcf059..4220cc034a3b 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -105,6 +105,7 @@ describe('.resolves', () => { expect(error.message).toMatchSnapshot(); }); }); + describe('.toBe()', () => { it('does not throw', () => { jestExpect('a').not.toBe('b'); @@ -114,6 +115,7 @@ describe('.toBe()', () => { jestExpect(null).not.toBe(undefined); jestExpect(null).toBe(null); jestExpect(undefined).toBe(undefined); + jestExpect(NaN).toBe(NaN); }); [ @@ -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(); diff --git a/packages/expect/src/matchers.js b/packages/expect/src/matchers.js index 66d2a7bf4215..c8a7bd2ecc35 100644 --- a/packages/expect/src/matchers.js +++ b/packages/expect/src/matchers.js @@ -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)}` @@ -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)}` + diff --git a/packages/jest-jasmine2/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/jest-jasmine2/src/__tests__/__snapshots__/matchers.test.js.snap index 3b46bc3937ff..06eadd13e8aa 100644 --- a/packages/jest-jasmine2/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/jest-jasmine2/src/__tests__/__snapshots__/matchers.test.js.snap @@ -3,7 +3,7 @@ exports[`matchers proxies matchers to expect 1`] = ` "expect(received).toBe(expected) -Expected value to be (using ===): +Expected value to be (using Object.is): 2 Received: 1"