diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bb41f9d7683..d507a365eedf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### Fixes - `[jest-config]` Treat `setupFilesAfterEnv` like `setupFiles` when normalizing configs against presets ([#9495](https://github.com/facebook/jest/pull/9495)) +- `[jest-matcher-utils]` Fix diff highlight of symbol-keyed object. ([#9499](https://github.com/facebook/jest/pull/9499)) - `[jest-snapshot]` Downgrade semver to v6 to support node 8 ([#9451](https://github.com/facebook/jest/pull/9451)) - `[jest-transform]` Correct sourcemap behavior for transformed and instrumented code ([#9460](https://github.com/facebook/jest/pull/9460)) - `[pretty-format]` Export `OldPlugin` type ([#9491](https://github.com/facebook/jest/pull/9491)) diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index c95358ee16aa..4c19643b51b9 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -2155,6 +2155,19 @@ Expected: 0 Received: {} `; +exports[`.toEqual() {pass: false} expect({Symbol(foo): 1, Symbol(bar): 2}).toEqual({Symbol(foo): Any, Symbol(bar): 1}) 1`] = ` +expect(received).toEqual(expected) // deep equality + +- Expected - 1 ++ Received + 1 + + Object { + Symbol(foo): Any, +- Symbol(bar): 1, ++ Symbol(bar): 2, + } +`; + exports[`.toEqual() {pass: false} expect(0).toEqual({}) 1`] = ` expect(received).toEqual(expected) // deep equality @@ -2622,6 +2635,13 @@ Expected: not {} `; +exports[`.toEqual() {pass: true} expect({Symbol(foo): 1, Symbol(bar): 2}).not.toEqual({Symbol(foo): Any, Symbol(bar): 2}) 1`] = ` +expect(received).not.toEqual(expected) // deep equality + +Expected: not {Symbol(foo): Any, Symbol(bar): 2} +Received: {Symbol(foo): 1, Symbol(bar): 2} +`; + exports[`.toEqual() {pass: true} expect(0).not.toEqual(0) 1`] = ` expect(received).not.toEqual(expected) // deep equality @@ -3778,6 +3798,19 @@ exports[`toMatchObject() {pass: false} expect({"a": "a", "c": "d"}).toMatchObjec } `; +exports[`toMatchObject() {pass: false} expect({"a": "b", "c": "d", Symbol(jest): "jest"}).toMatchObject({"a": "c", Symbol(jest): Any}) 1`] = ` +expect(received).toMatchObject(expected) + +- Expected - 2 ++ Received + 1 + + Object { +- "a": "c", +- Symbol(jest): Any, ++ "a": "b", + } +`; + exports[`toMatchObject() {pass: false} expect({"a": "b", "c": "d"}).toMatchObject({"a": "b!", "c": "d"}) 1`] = ` expect(received).toMatchObject(expected) @@ -4068,6 +4101,19 @@ exports[`toMatchObject() {pass: true} expect([Error: foo]).toMatchObject([Error: Expected: not [Error: foo] `; +exports[`toMatchObject() {pass: true} expect({"a": "b", "c": "d", Symbol(jest): "jest"}).toMatchObject({"a": "b", "c": "d", Symbol(jest): "jest"}) 1`] = ` +expect(received).not.toMatchObject(expected) + +Expected: not {"a": "b", "c": "d", Symbol(jest): "jest"} +`; + +exports[`toMatchObject() {pass: true} expect({"a": "b", "c": "d", Symbol(jest): "jest"}).toMatchObject({"a": "b", Symbol(jest): "jest"}) 1`] = ` +expect(received).not.toMatchObject(expected) + +Expected: not {"a": "b", Symbol(jest): "jest"} +Received: {"a": "b", "c": "d", Symbol(jest): "jest"} +`; + exports[`toMatchObject() {pass: true} expect({"a": "b", "c": "d"}).toMatchObject({"a": "b", "c": "d"}) 1`] = ` expect(received).not.toMatchObject(expected) diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index f177a98ed932..55d11a9bc8f9 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -529,6 +529,16 @@ describe('.toEqual()', () => { nodeType: 1, }, ], + [ + { + [Symbol.for('foo')]: 1, + [Symbol.for('bar')]: 2, + }, + { + [Symbol.for('foo')]: jestExpect.any(Number), + [Symbol.for('bar')]: 1, + }, + ], ].forEach(([a, b]) => { test(`{pass: false} expect(${stringify(a)}).toEqual(${stringify( b, @@ -727,6 +737,16 @@ describe('.toEqual()', () => { nodeType: 1, }, ], + [ + { + [Symbol.for('foo')]: 1, + [Symbol.for('bar')]: 2, + }, + { + [Symbol.for('foo')]: jestExpect.any(Number), + [Symbol.for('bar')]: 2, + }, + ], ].forEach(([a, b]) => { test(`{pass: true} expect(${stringify(a)}).not.toEqual(${stringify( b, @@ -2006,6 +2026,14 @@ describe('toMatchObject()', () => { [new Error('bar'), {message: 'bar'}], [new Foo(), {a: undefined, b: 'b'}], [Object.assign(Object.create(null), {a: 'b'}), {a: 'b'}], + [ + {a: 'b', c: 'd', [Symbol.for('jest')]: 'jest'}, + {a: 'b', [Symbol.for('jest')]: 'jest'}, + ], + [ + {a: 'b', c: 'd', [Symbol.for('jest')]: 'jest'}, + {a: 'b', c: 'd', [Symbol.for('jest')]: 'jest'}, + ], ]); testToMatchSnapshots([ @@ -2049,6 +2077,10 @@ describe('toMatchObject()', () => { ], [new Error('foo'), new Error('bar')], [Object.assign(Object.create(null), {a: 'b'}), {c: 'd'}], + [ + {a: 'b', c: 'd', [Symbol.for('jest')]: 'jest'}, + {a: 'c', [Symbol.for('jest')]: expect.any(String)}, + ], ]); [ diff --git a/packages/jest-matcher-utils/src/Replaceable.ts b/packages/jest-matcher-utils/src/Replaceable.ts index 24862f439f13..2d32879286b2 100644 --- a/packages/jest-matcher-utils/src/Replaceable.ts +++ b/packages/jest-matcher-utils/src/Replaceable.ts @@ -34,6 +34,12 @@ export default class Replaceable { Object.entries(this.object).forEach(([key, value]) => { cb(value, key, this.object); }); + Object.getOwnPropertySymbols(this.object).forEach(key => { + const descriptor = Object.getOwnPropertyDescriptor(this.object, key); + if ((descriptor as PropertyDescriptor).enumerable) { + cb(this.object[key], key, this.object); + } + }); } else { this.object.forEach(cb); } diff --git a/packages/jest-matcher-utils/src/__tests__/Replaceable.test.ts b/packages/jest-matcher-utils/src/__tests__/Replaceable.test.ts index cb3294296140..aa724bfe968e 100644 --- a/packages/jest-matcher-utils/src/__tests__/Replaceable.test.ts +++ b/packages/jest-matcher-utils/src/__tests__/Replaceable.test.ts @@ -99,11 +99,27 @@ describe('Replaceable', () => { describe('forEach', () => { test('object forEach', () => { - const replaceable = new Replaceable({a: 1, b: 2}); + const symbolKey = Symbol('jest'); + const object = {a: 1, b: 2, [symbolKey]: 3}; + const replaceable = new Replaceable(object); + const cb = jest.fn(); + replaceable.forEach(cb); + expect(cb.mock.calls.length).toBe(3); + expect(cb.mock.calls[0]).toEqual([1, 'a', object]); + expect(cb.mock.calls[1]).toEqual([2, 'b', object]); + expect(cb.mock.calls[2]).toEqual([3, symbolKey, object]); + }); + + test('object forEach do not iterate none enumerable symbol key', () => { + const symbolKey = Symbol('jest'); + const object = {a: 1, b: 2}; + Object.defineProperty(object, symbolKey, {enumerable: false}); + const replaceable = new Replaceable(object); const cb = jest.fn(); replaceable.forEach(cb); - expect(cb.mock.calls[0]).toEqual([1, 'a', {a: 1, b: 2}]); - expect(cb.mock.calls[1]).toEqual([2, 'b', {a: 1, b: 2}]); + expect(cb.mock.calls.length).toBe(2); + expect(cb.mock.calls[0]).toEqual([1, 'a', object]); + expect(cb.mock.calls[1]).toEqual([2, 'b', object]); }); test('array forEach', () => { diff --git a/packages/jest-matcher-utils/src/__tests__/__snapshots__/printDiffOrStringify.test.ts.snap b/packages/jest-matcher-utils/src/__tests__/__snapshots__/printDiffOrStringify.test.ts.snap index 990ea8705924..7e0674650dc6 100644 --- a/packages/jest-matcher-utils/src/__tests__/__snapshots__/printDiffOrStringify.test.ts.snap +++ b/packages/jest-matcher-utils/src/__tests__/__snapshots__/printDiffOrStringify.test.ts.snap @@ -85,6 +85,7 @@ exports[`printDiffOrStringify asymmetricMatcher jest asymmetricMatcher 1`] = ` }, - "g": true, + "g": false, + Symbol(h): Any, } `; diff --git a/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts b/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts index 450b69363396..41b61c4b9fdc 100644 --- a/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts +++ b/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts @@ -106,6 +106,7 @@ describe('printDiffOrStringify', () => { b: 'jest is awesome', }, g: true, + [Symbol.for('h')]: 'jest is awesome', }; const received = { a: 1, @@ -117,6 +118,7 @@ describe('printDiffOrStringify', () => { a: expect.any(Date), }), g: false, + [Symbol.for('h')]: expect.any(String), }; expect(testDiffOrStringify(expected, received)).toMatchSnapshot();