Skip to content

fix(expect): handle async errors in expect.soft #8145

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

Merged
merged 2 commits into from
Jun 11, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/expect/src/jest-extend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,17 @@ function JestExtendPlugin(
if (
result
&& typeof result === 'object'
&& result instanceof Promise
&& typeof (result as any).then === 'function'
) {
return result.then(({ pass, message, actual, expected }) => {
const thenable = result as PromiseLike<SyncExpectationResult>
return thenable.then(({ pass, message, actual, expected }) => {
if ((pass && isNot) || (!pass && !isNot)) {
throw new JestExtendError(message(), actual, expected)
}
})
}

const { pass, message, actual, expected } = result
const { pass, message, actual, expected } = result as SyncExpectationResult

if ((pass && isNot) || (!pass && !isNot)) {
throw new JestExtendError(message(), actual, expected)
Expand Down
27 changes: 20 additions & 7 deletions packages/expect/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Test } from '@vitest/runner/types'
import type { Assertion } from './types'
import { noop } from '@vitest/utils'
import { processError } from '@vitest/utils/error'

export function createAssertionMessage(
Expand Down Expand Up @@ -73,12 +74,19 @@ export function recordAsyncExpect(
return promise
}

function handleTestError(test: Test, err: unknown) {
test.result ||= { state: 'fail' }
test.result.state = 'fail'
test.result.errors ||= []
test.result.errors.push(processError(err))
}

export function wrapAssertion(
utils: Chai.ChaiUtils,
name: string,
fn: (this: Chai.AssertionStatic & Assertion, ...args: any[]) => void,
fn: (this: Chai.AssertionStatic & Assertion, ...args: any[]) => void | PromiseLike<void>,
) {
return function (this: Chai.AssertionStatic & Assertion, ...args: any[]): void {
return function (this: Chai.AssertionStatic & Assertion, ...args: any[]): void | PromiseLike<void> {
// private
if (name !== 'withTest') {
utils.flag(this, '_name', name)
Expand All @@ -95,13 +103,18 @@ export function wrapAssertion(
}

try {
return fn.apply(this, args)
const result = fn.apply(this, args)

if (result && typeof result === 'object' && typeof result.then === 'function') {
return result.then(noop, (err) => {
handleTestError(test, err)
})
}

return result
}
catch (err) {
test.result ||= { state: 'fail' }
test.result.state = 'fail'
test.result.errors ||= []
test.result.errors.push(processError(err))
handleTestError(test, err)
}
}
}
13 changes: 13 additions & 0 deletions test/cli/fixtures/expect-soft/expects/soft.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect, test } from 'vitest'

interface CustomMatchers<R = unknown> {
toBeAsync: (expected: unknown) => Promise<R>;
toBeDividedBy(divisor: number): R
}

Expand All @@ -9,6 +10,12 @@ declare module 'vitest' {
}

expect.extend({
toBeAsync: async function (received, expected) {
return {
pass: received === expected,
message: () => `expected ${received} to be ${expected} (asynchronously)`,
};
},
toBeDividedBy(received, divisor) {
const pass = received % divisor === 0
if (pass) {
Expand Down Expand Up @@ -62,6 +69,12 @@ test('with expect.extend', () => {
expect(5).toEqual(6)
})

test('promise with expect.extend', async () => {
await expect.soft(1 + 1).toBeAsync(3);
await expect.soft(1 + 2).toBeAsync(3);
await expect.soft(2 + 2).toBeAsync(3);
});

test('passed', () => {
expect.soft(1).toEqual(1)
expect(10).toEqual(10)
Expand Down
6 changes: 6 additions & 0 deletions test/cli/test/expect-soft.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ describe('expect.soft', () => {
expect.soft(stderr).toContain('AssertionError: expected 5 to deeply equal 6')
})

test('promise with expect.extend', async () => {
const { stderr } = await run()
expect.soft(stderr).toContain('Error: expected 2 to be 3')
expect.soft(stderr).toContain('Error: expected 4 to be 3')
})

test('passed', async () => {
const { stdout } = await run()
expect.soft(stdout).toContain('soft.test.ts > passed')
Expand Down