Skip to content

Commit 9423b6f

Browse files
authored
fix(expect): correctly show async matcher diff (#3960)
1 parent 5c88d8e commit 9423b6f

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

packages/utils/src/error.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { diff } from './diff'
22
import { format } from './display'
3-
import { getOwnProperties, getType } from './helpers'
3+
import { deepClone, getOwnProperties, getType } from './helpers'
44
import { stringify } from './stringify'
55

66
const IS_RECORD_SYMBOL = '@@__IMMUTABLE_RECORD__@@'
@@ -96,8 +96,13 @@ export function processError(err: any) {
9696
if (err.name)
9797
err.nameStr = String(err.name)
9898

99-
if (err.showDiff || (err.showDiff === undefined && err.expected !== undefined && err.actual !== undefined))
100-
err.diff = diff(err.expected, err.actual)
99+
if (err.showDiff || (err.showDiff === undefined && err.expected !== undefined && err.actual !== undefined)) {
100+
const clonedActual = deepClone(err.actual, { forceWritable: true })
101+
const clonedExpected = deepClone(err.expected, { forceWritable: true })
102+
103+
const { replacedActual, replacedExpected } = replaceAsymmetricMatcher(clonedActual, clonedExpected)
104+
err.diff = diff(replacedExpected, replacedActual)
105+
}
101106

102107
if (typeof err.expected !== 'string')
103108
err.expected = stringify(err.expected, 10)

test/core/test/jest-expect.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -787,4 +787,28 @@ it('correctly prints diff', () => {
787787
}
788788
})
789789

790+
it('correctly prints diff with asymmetric matchers', () => {
791+
try {
792+
expect({ a: 1, b: 'string' }).toEqual({
793+
a: expect.any(Number),
794+
b: expect.any(Function),
795+
})
796+
expect.unreachable()
797+
}
798+
catch (err) {
799+
setupColors(getDefaultColors())
800+
const error = processError(err)
801+
expect(error.diff).toMatchInlineSnapshot(`
802+
"- Expected
803+
+ Received
804+
805+
Object {
806+
\\"a\\": Any<Number>,
807+
- \\"b\\": Any<Function>,
808+
+ \\"b\\": \\"string\\",
809+
}"
810+
`)
811+
}
812+
})
813+
790814
it('timeout', () => new Promise(resolve => setTimeout(resolve, 500)))

0 commit comments

Comments
 (0)