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): correctly show async matcher diff #3960

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/utils/src/error.ts
@@ -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
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)))