Skip to content

Commit 3ffcd2e

Browse files
authoredFeb 13, 2024··
fix(utils): fix asymmetric matcher diff inside array (#5189)
1 parent 7d18233 commit 3ffcd2e

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed
 

‎packages/utils/src/error.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ function isAsymmetricMatcher(data: any) {
141141
function isReplaceable(obj1: any, obj2: any) {
142142
const obj1Type = getType(obj1)
143143
const obj2Type = getType(obj2)
144-
return obj1Type === obj2Type && obj1Type === 'Object'
144+
return obj1Type === obj2Type && (obj1Type === 'Object' || obj1Type === 'Array')
145145
}
146146

147147
export function replaceAsymmetricMatcher(actual: any, expected: any, actualReplaced = new WeakSet(), expectedReplaced = new WeakSet()) {

‎test/core/test/diff.test.ts

+66
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { expect, test, vi } from 'vitest'
22
import { getDefaultColors, setupColors } from '@vitest/utils'
33
import { diff } from '@vitest/utils/diff'
4+
import { processError } from '@vitest/runner'
45
import { displayDiff } from '../../../packages/vitest/src/node/error'
56

67
test('displays object diff', () => {
@@ -59,3 +60,68 @@ test('display multiline line string diff', () => {
5960
"
6061
`)
6162
})
63+
64+
test('asymmetric matcher in object', () => {
65+
setupColors(getDefaultColors())
66+
expect(getErrorDiff({ x: 0, y: 'foo' }, { x: 1, y: expect.anything() })).toMatchInlineSnapshot(`
67+
"- Expected
68+
+ Received
69+
70+
Object {
71+
- "x": 1,
72+
+ "x": 0,
73+
"y": Anything,
74+
}"
75+
`)
76+
})
77+
78+
test('asymmetric matcher in array', () => {
79+
setupColors(getDefaultColors())
80+
expect(getErrorDiff([0, 'foo'], [1, expect.anything()])).toMatchInlineSnapshot(`
81+
"- Expected
82+
+ Received
83+
84+
Array [
85+
- 1,
86+
+ 0,
87+
Anything,
88+
]"
89+
`)
90+
})
91+
92+
test('asymmetric matcher in nested', () => {
93+
setupColors(getDefaultColors())
94+
expect(
95+
getErrorDiff(
96+
[{ x: 0, y: 'foo' }, [0, 'bar']],
97+
[{ x: 1, y: expect.anything() }, [1, expect.anything()]],
98+
),
99+
).toMatchInlineSnapshot(`
100+
"- Expected
101+
+ Received
102+
103+
Array [
104+
Object {
105+
- "x": 1,
106+
+ "x": 0,
107+
"y": Anything,
108+
},
109+
Array [
110+
- 1,
111+
+ 0,
112+
Anything,
113+
],
114+
]"
115+
`)
116+
})
117+
118+
function getErrorDiff(actual: unknown, expected: unknown) {
119+
try {
120+
expect(actual).toEqual(expected)
121+
}
122+
catch (e) {
123+
const error = processError(e)
124+
return error.diff
125+
}
126+
expect.unreachable()
127+
}

0 commit comments

Comments
 (0)
Please sign in to comment.