From 769fa5898706f581b6fa39115f51f37ca12fc316 Mon Sep 17 00:00:00 2001 From: "ofek.meoded" Date: Mon, 29 Jan 2024 23:47:32 +0200 Subject: [PATCH] Fix not addressing to Sets as object without keys --- CHANGELOG.md | 1 + .../expect-utils/src/__tests__/utils.test.ts | 28 +++++++++++++++++++ packages/expect-utils/src/utils.ts | 3 +- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4060efb0059..bc2d62c1a100 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ - `[@jest/expect-utils]` [**BREAKING**] exclude non-enumerable in object matching ([#14670](https://github.com/jestjs/jest/pull/14670)) - `[@jest/expect-utils]` Fix comparison of `URL` ([#14672](https://github.com/jestjs/jest/pull/14672)) - `[@jest/expect-utils]` Check `Symbol` properties in equality ([#14688](https://github.com/jestjs/jest/pull/14688)) +- `[@jest/expect-utils]` Fix not addressing to Sets as objects without keys ([#14873](https://github.com/jestjs/jest/pull/14873)) - `[jest-leak-detector]` Make leak-detector more aggressive when running GC ([#14526](https://github.com/jestjs/jest/pull/14526)) - `[jest-runtime]` Properly handle re-exported native modules in ESM via CJS ([#14589](https://github.com/jestjs/jest/pull/14589)) - `[jest-util]` Make sure `isInteractive` works in a browser ([#14552](https://github.com/jestjs/jest/pull/14552)) diff --git a/packages/expect-utils/src/__tests__/utils.test.ts b/packages/expect-utils/src/__tests__/utils.test.ts index 9adce1b362a4..0eea9741dee8 100644 --- a/packages/expect-utils/src/__tests__/utils.test.ts +++ b/packages/expect-utils/src/__tests__/utils.test.ts @@ -401,6 +401,34 @@ describe('subsetEquality()', () => { }); }); }); + + describe('subset is not object with keys', () => { + test('returns true if subset has keys', () => { + expect(subsetEquality({foo: 'bar'}, {foo: 'bar'})).toBe(true); + }); + test('returns true if subset has Symbols', () => { + const symbol = Symbol('foo'); + expect(subsetEquality({[symbol]: 'bar'}, {[symbol]: 'bar'})).toBe(true); + }); + test('returns undefined if subset has no keys', () => { + expect(subsetEquality('foo', 'bar')).toBeUndefined(); + }); + test('returns undefined if subset is null', () => { + expect(subsetEquality({foo: 'bar'}, null)).toBeUndefined(); + }); + test('returns undefined if subset is Error', () => { + expect(subsetEquality({foo: 'bar'}, new Error())).toBeUndefined(); + }); + test('returns undefined if subset is Array', () => { + expect(subsetEquality({foo: 'bar'}, [])).toBeUndefined(); + }); + test('returns undefined if subset is Date', () => { + expect(subsetEquality({foo: 'bar'}, new Date())).toBeUndefined(); + }); + test('returns undefined if subset is Set', () => { + expect(subsetEquality({foo: 'bar'}, new Set())).toBeUndefined(); + }); + }); }); describe('iterableEquality', () => { diff --git a/packages/expect-utils/src/utils.ts b/packages/expect-utils/src/utils.ts index 4872e4a5c7b5..73955f6c169b 100644 --- a/packages/expect-utils/src/utils.ts +++ b/packages/expect-utils/src/utils.ts @@ -336,7 +336,8 @@ const isObjectWithKeys = (a: any) => isObject(a) && !(a instanceof Error) && !Array.isArray(a) && - !(a instanceof Date); + !(a instanceof Date) && + !(a instanceof Set); export const subsetEquality = ( object: unknown,