Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/to equal undefined comparison #7111

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions packages/expect/src/jest-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ export function equals(
b: unknown,
customTesters?: Array<Tester>,
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
Expand Down Expand Up @@ -93,6 +94,7 @@ function eq(
bStack: Array<unknown>,
customTesters: Array<Tester>,
hasKey: any,
ignoreUndefined = true,
): boolean {
let result = true

Expand Down Expand Up @@ -207,23 +209,37 @@ 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) {
return false
}
}

// Remove the first object from the stack of traversed objects.
aStack.pop()
bStack.pop()
Expand Down
9 changes: 9 additions & 0 deletions packages/ui/client/auto-imports.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
32 changes: 32 additions & 0 deletions test/core/test/jest-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1849,3 +1849,35 @@ it('diff', () => {
snapshotError(() => expect({ hello: 'world' }).toBeUndefined())
snapshotError(() => expect({ hello: 'world' }).toBeNull())
})

describe('undefined property handling', () => {
it('toEqual ignores undefined properties', () => {
const obj1 = {
foo: 'bar',
qux: 'qux',
}

const obj2 = {
foo: 'bar',
extra: undefined,
qux: 'qux',
}

expect(obj1).toEqual(obj2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add tests for toEqual error diff? I think that's the issue raised by #7083

snapshotError(() =>
  expect({
    x: 'foo',
    y: undefined,
  }).toEqual({
    x: 'bar',
  })
);

snapshotError(() =>
  expect({
    x: 'foo',
  }).toEqual({
    x: 'bar',
    y: undefined,
  })
);

})

it('toStrictEqual considers undefined properties', () => {
const obj1 = {
foo: 'bar',
qux: 'qux',
}

const obj2 = {
foo: 'bar',
extra: undefined,
qux: 'qux',
}

expect(() => expect(obj1).toStrictEqual(obj2)).toThrow()
})
})
Loading