Skip to content

Commit 4277075

Browse files
hi-ogawaclaude
andauthored
fix(expect): keep soft .resolves/.rejects error location (#11792)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 488bfe1 commit 4277075

3 files changed

Lines changed: 65 additions & 3 deletions

File tree

‎packages/expect/src/utils.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ export function wrapAssertion(
106106
utils.flag(this, '_name', name)
107107
}
108108

109-
if (!utils.flag(this, 'soft')) {
109+
// `.resolves/rejects` handle `expect.soft` through `recordAsyncExpect`
110+
const isSoft = utils.flag(this, 'soft') && !utils.flag(this, 'promise')
111+
if (!isSoft) {
110112
// avoid WebKit's proper tail call to preserve stacktrace offset for inline snapshot
111113
// https://webkit.org/blog/6240/ecmascript-6-proper-tail-calls-in-webkit
112114
try {

‎packages/vitest/src/integrations/snapshot/chai.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,14 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => {
121121
const assertPromise = resultPromise.then((result) =>
122122
assertMatchResult(result, chai.util.flag(this, 'message')),
123123
)
124+
// `.resolves/rejects` handle `expect.soft` through `recordAsyncExpect`
125+
const isSoft = utils.flag(this, 'soft') && !utils.flag(this, 'promise')
124126
return recordAsyncExpect(
125127
getTest(this),
126128
assertPromise,
127129
createAssertionMessage(utils, this, true),
128130
new Error('resolves'),
129-
utils.flag(this, 'soft'),
131+
isSoft,
130132
)
131133
},
132134
)

‎test/e2e/test/expect-soft.test.ts‎

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { TestUserConfig } from 'vitest/node'
22
import { resolve } from 'node:path'
33
import { describe, expect, test, TestRunner } from 'vitest'
4-
import { runVitest } from '../../test-utils'
4+
import { runInlineTests, runVitest } from '../../test-utils'
55

66
describe('expect.soft', () => {
77
const run = (config?: TestUserConfig) =>
@@ -82,3 +82,61 @@ describe('expect.soft', () => {
8282
expect.soft(stderr).toContain('4/4')
8383
})
8484
})
85+
86+
test('expect.soft with .resolves and .rejects reports the assertion location', async () => {
87+
const { stderr, buildTree } = await runInlineTests(
88+
{
89+
'basic.test.ts': `
90+
import { expect, test } from 'vitest'
91+
92+
expect.extend({
93+
toBeOld(received) {
94+
return { pass: received === 'old', message: () => 'expected ' + received + ' to be old' }
95+
},
96+
})
97+
98+
test('resolves', async () => {
99+
await expect.soft(Promise.resolve('new')).resolves.toBe('old')
100+
await expect.soft(Promise.resolve('new')).resolves.toBeOld()
101+
})
102+
103+
test('rejects', async () => {
104+
await expect.soft(Promise.reject(new Error('new'))).rejects.toThrow('old')
105+
await expect.soft(Promise.reject(new Error('new'))).rejects.toMatchInlineSnapshot(\`[Error: old]\`)
106+
await expect.soft(Promise.reject(new Error('new'))).rejects.toMatchFileSnapshot('./old.txt')
107+
})
108+
109+
test('mismatch', async () => {
110+
await expect.soft(Promise.resolve('new')).rejects.toThrow('old')
111+
await expect.soft(Promise.reject(new Error('new'))).resolves.toBe('old')
112+
})
113+
`,
114+
'old.txt': 'old',
115+
},
116+
{ update: 'none' },
117+
)
118+
expect(stderr).not.toBe('')
119+
expect(
120+
buildTree((testCase) =>
121+
testCase.result().errors?.map((e) => `${e.stacks?.[0]?.line}: ${e.message}`),
122+
),
123+
).toMatchInlineSnapshot(`
124+
{
125+
"basic.test.ts": {
126+
"mismatch": [
127+
"22: promise resolved "'new'" instead of rejecting",
128+
"23: promise rejected "Error: new" instead of resolving",
129+
],
130+
"rejects": [
131+
"16: expected [Function] to throw error including 'old' but got 'new'",
132+
"17: Snapshot \`rejects 1\` mismatched",
133+
"18: Snapshot \`rejects 2\` mismatched",
134+
],
135+
"resolves": [
136+
"11: expected 'new' to be 'old' // Object.is equality",
137+
"12: expected new to be old",
138+
],
139+
},
140+
}
141+
`)
142+
})

0 commit comments

Comments
 (0)