|
1 | 1 | import type { TestUserConfig } from 'vitest/node' |
2 | 2 | import { resolve } from 'node:path' |
3 | 3 | import { describe, expect, test, TestRunner } from 'vitest' |
4 | | -import { runVitest } from '../../test-utils' |
| 4 | +import { runInlineTests, runVitest } from '../../test-utils' |
5 | 5 |
|
6 | 6 | describe('expect.soft', () => { |
7 | 7 | const run = (config?: TestUserConfig) => |
@@ -82,3 +82,61 @@ describe('expect.soft', () => { |
82 | 82 | expect.soft(stderr).toContain('4/4') |
83 | 83 | }) |
84 | 84 | }) |
| 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