fix(expect): soft assertions continue after .resolves/.rejects promise errors#9843
Open
mixelburg wants to merge 2 commits intovitest-dev:mainfrom
Open
fix(expect): soft assertions continue after .resolves/.rejects promise errors#9843mixelburg wants to merge 2 commits intovitest-dev:mainfrom
mixelburg wants to merge 2 commits intovitest-dev:mainfrom
Conversation
…s proxies When expect.soft() is used with .resolves, a rejection in the awaited promise was not handled softly. Instead of recording the failure and continuing (as expect.soft() should), the rejection propagated to the test, aborting execution of subsequent assertions. Root cause: the .resolves and .rejects proxy callbacks call recordAsyncExpect() without passing the isSoft flag. When isSoft is false/undefined, recordAsyncExpect does not attach a rejection handler to swallow the error and record it in test.result.errors; the returned promise still rejects, so awaiting it terminates the test. Fix: pass utils.flag(this, 'soft') as the isSoft argument to both recordAsyncExpect calls (in the resolves proxy and in the rejects proxy). The same fix also corrects the symmetric case for .rejects: if expect.soft(resolvedPromise).rejects is used, the 'resolved instead of rejecting' error is now recorded softly and execution continues. Fixes vitest-dev#9838
Contributor
|
CI failing. fix please. |
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
✅ Deploy Preview for vitest-dev ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
When
expect.soft()is combined with.resolvesand the awaited promise rejects, the test aborts instead of recording the failure and continuing (which is whatexpect.softis designed to do).The symmetric case also fails:
expect.soft(resolvedPromise).rejectsaborts when the promise resolves.Fixes #9838
Root cause
Inside both the
.resolvesand.rejectsproxy accessors, the method interceptor creates a new promise (thePromise.resolve(obj).then(...).catch(...)chain), then hands it torecordAsyncExpect:recordAsyncExpecthas a specialisSoftbranch:Without
isSoft, the rejection propagates through the returned promise wrapper; when the callerawaits it, the test runner sees an unhandled rejection and aborts the test.Fix
Pass
utils.flag(this, 'soft')(which is already set byexpect.soft()) as the fifth argument in bothrecordAsyncExpectcalls.Tests added
Added two new fixture test cases to
test/cli/fixtures/expect-soft/expects/soft.test.ts:promise rejection— two consecutiveexpect.soft(rejectedPromise).resolves.toBe(...)assertions; both should execute and both failures should be reported (regression for issue)promise resolved instead of rejecting— two consecutiveexpect.soft(resolvedPromise).rejects.toBe(...)assertions; same expectation for symmetric caseAnd corresponding expectations in
test/cli/test/expect-soft.test.tsthat verify both failure messages appear in stderr.