From 4f570c4fa440e2a81c18f5e679c9fc8c96268d55 Mon Sep 17 00:00:00 2001 From: Shoaib Akhtar Date: Sat, 21 Dec 2024 11:48:30 +0530 Subject: [PATCH 1/4] fix(expect): handle undefined properties consistently in toEqual matcher --- .vscode/settings.json | 3 ++- packages/expect/src/jest-utils.ts | 27 ++++++++++++++++++------- test/core/test/jest-expect.test.ts | 32 ++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 8da1bd8263a0..f7ee9a14c200 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -45,5 +45,6 @@ "json", "jsonc", "yaml" - ] + ], + "testing.automaticallyOpenTestResults": "neverOpen" } diff --git a/packages/expect/src/jest-utils.ts b/packages/expect/src/jest-utils.ts index 248bd9f79b44..05c648b6b572 100644 --- a/packages/expect/src/jest-utils.ts +++ b/packages/expect/src/jest-utils.ts @@ -31,9 +31,10 @@ export function equals( b: unknown, customTesters?: Array, strictCheck?: boolean, + ignoreUndefined = !strictCheck, ): boolean { customTesters = customTesters || [] - return eq(a, b, [], [], customTesters, strictCheck ? hasKey : hasDefinedKey) + return eq(a, b, [], [], customTesters, strictCheck ? hasKey : hasDefinedKey, ignoreUndefined) } const functionToString = Function.prototype.toString @@ -93,6 +94,7 @@ function eq( bStack: Array, customTesters: Array, hasKey: any, + ignoreUndefined = true, ): boolean { let result = true @@ -207,23 +209,34 @@ function eq( let key let size = aKeys.length + // Get keys for b, filtering out undefined values if ignoreUndefined is true + const bKeys = keys(b, hasKey).filter(k => !ignoreUndefined || b[k] !== undefined) + // Ensure that both objects contain the same number of properties before comparing deep equality. - if (keys(b, hasKey).length !== size) { + if (ignoreUndefined) { + const filteredAKeys = aKeys.filter(k => a[k] !== undefined) + if (filteredAKeys.length !== bKeys.length) + return false + } + else if (bKeys.length !== size) { return false } while (size--) { key = aKeys[size] + // Skip undefined values when ignoreUndefined is true + if (ignoreUndefined && a[key] === undefined) + continue + // Deep compare each member - result - = hasKey(b, key) - && eq(a[key], b[key], aStack, bStack, customTesters, hasKey) + result = hasKey(b, key) + && eq(a[key], b[key], aStack, bStack, customTesters, hasKey, ignoreUndefined) - if (!result) { + if (!result) return false - } } + // Remove the first object from the stack of traversed objects. aStack.pop() bStack.pop() diff --git a/test/core/test/jest-expect.test.ts b/test/core/test/jest-expect.test.ts index d1f2a635d3a2..b20e984dab9a 100644 --- a/test/core/test/jest-expect.test.ts +++ b/test/core/test/jest-expect.test.ts @@ -1849,3 +1849,35 @@ it('diff', () => { snapshotError(() => expect({ hello: 'world' }).toBeUndefined()) snapshotError(() => expect({ hello: 'world' }).toBeNull()) }) + +describe('undefined property handling', () => { + test('toEqual ignores undefined properties', () => { + const obj1 = { + foo: 'bar', + qux: 'qux' + } + + const obj2 = { + foo: 'bar', + extra: undefined, + qux: 'qux' + } + + expect(obj1).toEqual(obj2) + }) + + test('toStrictEqual considers undefined properties', () => { + const obj1 = { + foo: 'bar', + qux: 'qux' + } + + const obj2 = { + foo: 'bar', + extra: undefined, + qux: 'qux' + } + + expect(() => expect(obj1).toStrictEqual(obj2)).toThrow() + }) +}) From b8bab94a42317f491e1eae89c4977d00e691d276 Mon Sep 17 00:00:00 2001 From: Shoaib Akhtar Date: Sat, 21 Dec 2024 12:06:16 +0530 Subject: [PATCH 2/4] fix(expect): handle undefined properties consistently in toEqual matcher --- packages/ui/client/auto-imports.d.ts | 9 +++++++++ test/core/test/jest-expect.test.ts | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/ui/client/auto-imports.d.ts b/packages/ui/client/auto-imports.d.ts index e5a11a01d5a7..033546ed30f2 100644 --- a/packages/ui/client/auto-imports.d.ts +++ b/packages/ui/client/auto-imports.d.ts @@ -344,4 +344,13 @@ declare global { // @ts-ignore export type { Component, ComponentPublicInstance, ComputedRef, DirectiveBinding, ExtractDefaultPropTypes, ExtractPropTypes, ExtractPublicPropTypes, InjectionKey, PropType, Ref, MaybeRef, MaybeRefOrGetter, VNode, WritableComputedRef } from 'vue' import('vue') + // @ts-ignore + export type { ViewportSize } from './composables/browser' + import('./composables/browser') + // @ts-ignore + export type { ModuleType, ModuleNode, ModuleLink, ModuleGraph, ModuleGraphController, ModuleGraphConfig, ModuleLabelItem } from './composables/module-graph' + import('./composables/module-graph') + // @ts-ignore + export type { Params } from './composables/params' + import('./composables/params') } diff --git a/test/core/test/jest-expect.test.ts b/test/core/test/jest-expect.test.ts index b20e984dab9a..dd7edb6b4adb 100644 --- a/test/core/test/jest-expect.test.ts +++ b/test/core/test/jest-expect.test.ts @@ -1851,7 +1851,7 @@ it('diff', () => { }) describe('undefined property handling', () => { - test('toEqual ignores undefined properties', () => { + it('toEqual ignores undefined properties', () => { const obj1 = { foo: 'bar', qux: 'qux' @@ -1866,7 +1866,7 @@ describe('undefined property handling', () => { expect(obj1).toEqual(obj2) }) - test('toStrictEqual considers undefined properties', () => { + it('toStrictEqual considers undefined properties', () => { const obj1 = { foo: 'bar', qux: 'qux' From a84fd236b62792efbf8dbb1f9103f3b49f4b817d Mon Sep 17 00:00:00 2001 From: Shoaib Akhtar Date: Sat, 21 Dec 2024 12:42:26 +0530 Subject: [PATCH 3/4] fixed lint issues --- .vscode/settings.json | 3 +-- packages/expect/src/jest-utils.ts | 12 +++++++----- test/core/test/jest-expect.test.ts | 12 ++++++------ 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index f7ee9a14c200..8da1bd8263a0 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -45,6 +45,5 @@ "json", "jsonc", "yaml" - ], - "testing.automaticallyOpenTestResults": "neverOpen" + ] } diff --git a/packages/expect/src/jest-utils.ts b/packages/expect/src/jest-utils.ts index 05c648b6b572..1cb8acd00b50 100644 --- a/packages/expect/src/jest-utils.ts +++ b/packages/expect/src/jest-utils.ts @@ -215,10 +215,10 @@ function eq( // Ensure that both objects contain the same number of properties before comparing deep equality. if (ignoreUndefined) { const filteredAKeys = aKeys.filter(k => a[k] !== undefined) - if (filteredAKeys.length !== bKeys.length) + if (filteredAKeys.length !== bKeys.length) { return false - } - else if (bKeys.length !== size) { + } + } else if (bKeys.length !== size) { return false } @@ -226,15 +226,17 @@ function eq( key = aKeys[size] // Skip undefined values when ignoreUndefined is true - if (ignoreUndefined && a[key] === undefined) + if (ignoreUndefined && a[key] === undefined) { continue + } // Deep compare each member result = hasKey(b, key) && eq(a[key], b[key], aStack, bStack, customTesters, hasKey, ignoreUndefined) - if (!result) + if (!result) { return false + } } // Remove the first object from the stack of traversed objects. diff --git a/test/core/test/jest-expect.test.ts b/test/core/test/jest-expect.test.ts index dd7edb6b4adb..b304c5b41575 100644 --- a/test/core/test/jest-expect.test.ts +++ b/test/core/test/jest-expect.test.ts @@ -1854,13 +1854,13 @@ describe('undefined property handling', () => { it('toEqual ignores undefined properties', () => { const obj1 = { foo: 'bar', - qux: 'qux' + qux: 'qux', } - + const obj2 = { foo: 'bar', extra: undefined, - qux: 'qux' + qux: 'qux', } expect(obj1).toEqual(obj2) @@ -1869,13 +1869,13 @@ describe('undefined property handling', () => { it('toStrictEqual considers undefined properties', () => { const obj1 = { foo: 'bar', - qux: 'qux' + qux: 'qux', } - + const obj2 = { foo: 'bar', extra: undefined, - qux: 'qux' + qux: 'qux', } expect(() => expect(obj1).toStrictEqual(obj2)).toThrow() From 723669d799d8c0f8473776aaa9afb5905d3e2088 Mon Sep 17 00:00:00 2001 From: Shoaib Akhtar Date: Sat, 21 Dec 2024 12:54:01 +0530 Subject: [PATCH 4/4] fixed lint errors --- packages/expect/src/jest-utils.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/expect/src/jest-utils.ts b/packages/expect/src/jest-utils.ts index 1cb8acd00b50..d4327b15f992 100644 --- a/packages/expect/src/jest-utils.ts +++ b/packages/expect/src/jest-utils.ts @@ -218,7 +218,8 @@ function eq( if (filteredAKeys.length !== bKeys.length) { return false } - } else if (bKeys.length !== size) { + } + else if (bKeys.length !== size) { return false } @@ -232,7 +233,7 @@ function eq( // Deep compare each member result = hasKey(b, key) - && eq(a[key], b[key], aStack, bStack, customTesters, hasKey, ignoreUndefined) + && eq(a[key], b[key], aStack, bStack, customTesters, hasKey, ignoreUndefined) if (!result) { return false