Skip to content

Commit

Permalink
fix(expect): correctly show async matcher diff (#3960)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Aug 16, 2023
1 parent 5c88d8e commit 9423b6f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/utils/src/error.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { diff } from './diff'
import { format } from './display'
import { getOwnProperties, getType } from './helpers'
import { deepClone, getOwnProperties, getType } from './helpers'
import { stringify } from './stringify'

const IS_RECORD_SYMBOL = '@@__IMMUTABLE_RECORD__@@'
Expand Down Expand Up @@ -96,8 +96,13 @@ export function processError(err: any) {
if (err.name)
err.nameStr = String(err.name)

if (err.showDiff || (err.showDiff === undefined && err.expected !== undefined && err.actual !== undefined))
err.diff = diff(err.expected, err.actual)
if (err.showDiff || (err.showDiff === undefined && err.expected !== undefined && err.actual !== undefined)) {
const clonedActual = deepClone(err.actual, { forceWritable: true })
const clonedExpected = deepClone(err.expected, { forceWritable: true })

const { replacedActual, replacedExpected } = replaceAsymmetricMatcher(clonedActual, clonedExpected)
err.diff = diff(replacedExpected, replacedActual)
}

if (typeof err.expected !== 'string')
err.expected = stringify(err.expected, 10)
Expand Down
24 changes: 24 additions & 0 deletions test/core/test/jest-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -787,4 +787,28 @@ it('correctly prints diff', () => {
}
})

it('correctly prints diff with asymmetric matchers', () => {
try {
expect({ a: 1, b: 'string' }).toEqual({
a: expect.any(Number),
b: expect.any(Function),
})
expect.unreachable()
}
catch (err) {
setupColors(getDefaultColors())
const error = processError(err)
expect(error.diff).toMatchInlineSnapshot(`
"- Expected
+ Received
Object {
\\"a\\": Any<Number>,
- \\"b\\": Any<Function>,
+ \\"b\\": \\"string\\",
}"
`)
}
})

it('timeout', () => new Promise(resolve => setTimeout(resolve, 500)))

0 comments on commit 9423b6f

Please sign in to comment.