From 6d74b494810ca78921e663c8b6937ef6c6f96e7e Mon Sep 17 00:00:00 2001 From: mixelburg <52622705+mixelburg@users.noreply.github.com> Date: Mon, 16 Mar 2026 05:40:48 +0200 Subject: [PATCH] fix(expect): soft assertions continue after .resolves/.rejects promise errors (#9843) Co-authored-by: Maks Pikov Co-authored-by: Claude Opus 4.6 Co-authored-by: Hiroshi Ogawa --- packages/expect/src/jest-expect.ts | 2 ++ .../fixtures/expect-soft/expects/soft.test.ts | 23 +++++++++++++++++++ test/cli/test/expect-soft.test.ts | 14 +++++++++++ 3 files changed, 39 insertions(+) diff --git a/packages/expect/src/jest-expect.ts b/packages/expect/src/jest-expect.ts index 8f6012fd6..340dec122 100644 --- a/packages/expect/src/jest-expect.ts +++ b/packages/expect/src/jest-expect.ts @@ -1128,6 +1128,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => { promise, createAssertionMessage(utils, this, !!args.length), error, + utils.flag(this, 'soft'), ) } }, @@ -1203,6 +1204,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => { promise, createAssertionMessage(utils, this, !!args.length), error, + utils.flag(this, 'soft'), ) } }, diff --git a/test/cli/fixtures/expect-soft/expects/soft.test.ts b/test/cli/fixtures/expect-soft/expects/soft.test.ts index 6d72e483c..e346e5883 100644 --- a/test/cli/fixtures/expect-soft/expects/soft.test.ts +++ b/test/cli/fixtures/expect-soft/expects/soft.test.ts @@ -75,6 +75,29 @@ test('promise with expect.extend', async () => { await expect.soft(2 + 2).toBeAsync(3); }); +test('promise rejection', async () => { + await expect + .soft( + Promise.resolve().then(() => { + throw new Error('boom 1st') + }), + ) + .resolves.toBe('1st') + + await expect + .soft( + Promise.resolve().then(() => { + throw new Error('boom 2nd') + }), + ) + .resolves.toBe('2nd') +}) + +test('promise resolved instead of rejecting', async () => { + await expect.soft(Promise.resolve('value 1')).rejects.toBe('1st') + await expect.soft(Promise.resolve('value 2')).rejects.toBe('2nd') +}) + test('passed', () => { expect.soft(1).toEqual(1) expect(10).toEqual(10) diff --git a/test/cli/test/expect-soft.test.ts b/test/cli/test/expect-soft.test.ts index 493556216..cbc04b806 100644 --- a/test/cli/test/expect-soft.test.ts +++ b/test/cli/test/expect-soft.test.ts @@ -45,6 +45,20 @@ describe('expect.soft', () => { expect.soft(stderr).toContain('Error: expected 4 to be 3') }) + test('promise rejection', async () => { + const { stderr } = await run() + // both assertions should execute (not abort after first rejection) + expect.soft(stderr).toContain('promise rejected "Error: boom 1st" instead of resolving') + expect.soft(stderr).toContain('promise rejected "Error: boom 2nd" instead of resolving') + }) + + test('promise resolved instead of rejecting', async () => { + const { stderr } = await run() + // both assertions should execute + expect.soft(stderr).toContain('promise resolved "\'value 1\'" instead of rejecting') + expect.soft(stderr).toContain('promise resolved "\'value 2\'" instead of rejecting') + }) + test('passed', async () => { const { stdout } = await run() expect.soft(stdout).toContain('soft.test.ts > passed') -- 2.51.2