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(expect): Fix to properly compare ObjectContaining #5862

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
c4a531b
chore: resolve conflict
pengooseDev Jun 16, 2024
c2f8492
Merge branch 'main' of https://github.com/pengooseDev/vitest into fix…
pengooseDev Jun 18, 2024
8b796e2
refactor: move filter logic(ObjectContaining) to utils module
pengooseDev Jun 18, 2024
2a09861
chore: revert previous changes
pengooseDev Jun 18, 2024
2ee8624
Merge branch 'main' of https://github.com/pengooseDev/vitest into fix…
pengooseDev Jun 20, 2024
3eed75a
feat: move diff logic to display
pengooseDev Jun 20, 2024
2a9231a
Merge branch 'vitest-dev:main' into fix-object-containing
pengooseDev Jun 20, 2024
47d9e41
Merge branch 'main' of https://github.com/pengooseDev/vitest into fix…
pengooseDev Jun 20, 2024
649fd22
Merge branch 'fix-object-containing' of https://github.com/pengooseDe…
pengooseDev Jun 20, 2024
d4311a9
Merge branch 'main' of https://github.com/pengooseDev/vitest into fix…
pengooseDev Jun 20, 2024
376fc0a
Merge branch 'main' of https://github.com/pengooseDev/vitest into fix…
pengooseDev Jun 25, 2024
bd09ddd
Merge branch 'fix-object-containing' of https://github.com/pengooseDe…
pengooseDev Jun 25, 2024
bc4124b
Merge branch 'main' of https://github.com/pengooseDev/vitest into fix…
pengooseDev Jun 26, 2024
7dd59d1
Merge branch 'main' of https://github.com/pengooseDev/vitest into fix…
pengooseDev Jun 27, 2024
ff9b42e
Merge branch 'fix-object-containing' of https://github.com/pengooseDe…
pengooseDev Jun 27, 2024
b3c601b
Merge branch 'main' into fix-object-containing
sheremet-va Jul 1, 2024
637a5dc
Merge branch 'vitest-dev:main' into fix-object-containing
pengooseDev Jul 1, 2024
8f7b97b
chore: remove unused interface props
pengooseDev Jul 1, 2024
c8caaf1
Merge branch 'fix-object-containing' of https://github.com/pengooseDe…
pengooseDev Jul 1, 2024
72647b2
Merge branch 'main' of https://github.com/pengooseDev/vitest into fix…
pengooseDev Jul 1, 2024
2ef4935
Merge branch 'vitest-dev:main' into fix-object-containing
pengooseDev Jul 1, 2024
914b4c1
chore: remove deepClone
pengooseDev Jul 1, 2024
b6990ee
Merge branch 'fix-object-containing' of https://github.com/pengooseDe…
pengooseDev Jul 1, 2024
1ffe9e3
Merge branch 'vitest-dev:main' into fix-object-containing
pengooseDev Jul 1, 2024
cc22952
Merge branch 'fix-object-containing' of https://github.com/pengooseDe…
pengooseDev Jul 1, 2024
a9dbb58
Merge branch 'main' into fix-object-containing
pengooseDev Jul 1, 2024
7193f31
Merge branch 'main' into fix-object-containing
pengooseDev Jul 1, 2024
b44853e
Merge branch 'main' into fix-object-containing
pengooseDev Jul 3, 2024
9cf7311
Merge branch 'main' into fix-object-containing
pengooseDev Jul 5, 2024
eeb0a4d
Merge branch 'main' into fix-object-containing
pengooseDev Jul 6, 2024
fa73aea
Merge branch 'main' into fix-object-containing
pengooseDev Jul 8, 2024
dff368f
Merge branch 'main' into fix-object-containing
pengooseDev Jul 9, 2024
3552a35
Merge branch 'main' into fix-object-containing
pengooseDev Jul 10, 2024
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: 26 additions & 0 deletions packages/utils/src/diff/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
format as prettyFormat,
plugins as prettyFormatPlugins,
} from 'pretty-format'
import { isObjectContaining } from '../helpers'
import { getType } from './getType'
import { DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, Diff } from './cleanupSemantic'
import { NO_DIFF_MESSAGE, SIMILAR_MESSAGE } from './constants'
Expand Down Expand Up @@ -185,6 +186,18 @@ function getFormatOptions(
}
}

function replaceDiffData(a: any, b: any, expected?: string) {
if (isObjectContaining(expected)) {
Object.keys(b).forEach((key) => {
if (!Object.prototype.hasOwnProperty.call(a.sample, key)) {
a.sample[key] = b[key]
}
})
}

return { replacedA: a, replacedB: b }
}

function getObjectsDifference(
a: Record<string, any>,
b: Record<string, any>,
Expand All @@ -198,6 +211,19 @@ function getObjectsDifference(
if (aCompare === bCompare) {
return getCommonMessage(NO_DIFF_MESSAGE, options)
}
else if (options?.expected) {
const { replacedA, replacedB } = replaceDiffData(a, b, options?.expected)
const aDisplay = prettyFormat(replacedA, formatOptions)
const bDisplay = prettyFormat(replacedB, formatOptions)

return diffLinesUnified2(
aDisplay.split('\n'),
bDisplay.split('\n'),
aCompare.split('\n'),
bCompare.split('\n'),
options,
)
}
else {
const aDisplay = prettyFormat(a, formatOptions)
const bDisplay = prettyFormat(b, formatOptions)
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/diff/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface DiffOptions {
contextLines?: number
emptyFirstOrLastLinePlaceholder?: string
expand?: boolean
expected?: string
includeChangeCounts?: boolean
omitAnnotationLines?: boolean
patchColor?: DiffOptionsColor
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export function processError(
err.diff = diff(replacedExpected, replacedActual, {
...diffOptions,
...err.diffOptions,
expected: err.expected,
})
}

Expand Down
6 changes: 6 additions & 0 deletions packages/utils/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,9 @@ export function isNegativeNaN(val: number) {

return isNegative
}

export function isObjectContaining(expected: any): boolean {
return typeof expected === 'object'
&& typeof expected.asymmetricMatch === 'function'
&& (expected.toString() === 'ObjectContaining' || expected.toString() === 'ObjectNotContaining')
}
2 changes: 1 addition & 1 deletion test/core/test/__snapshots__/jest-expect.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ exports[`asymmetric matcher error 12`] = `
- ObjectContaining {
+ Object {
"k": "v",
"k2": "v2",
- "k3": "v3",
+ "k2": "v2",
}",
"expected": "ObjectContaining {
"k": "v",
Expand Down
Loading