|
| 1 | +import { expect, test } from 'vitest' |
| 2 | +import { runInlineTests } from '../../test-utils' |
| 3 | + |
| 4 | +test('file fixture whose setup failed is set up again on retry', async () => { |
| 5 | + const { stdout, errorTree } = await runInlineTests({ |
| 6 | + 'basic.test.ts': ` |
| 7 | + import { expect, test } from 'vitest' |
| 8 | +
|
| 9 | + let attempts = 0 |
| 10 | +
|
| 11 | + const extended = test.extend<{ file: string }>({ |
| 12 | + file: [ |
| 13 | + async ({}, use) => { |
| 14 | + attempts++ |
| 15 | + console.log('>> init file', attempts) |
| 16 | + if (attempts === 1) { |
| 17 | + throw new Error('setup failed once') |
| 18 | + } |
| 19 | + await use('file') |
| 20 | + }, |
| 21 | + { scope: 'file' }, |
| 22 | + ], |
| 23 | + }) |
| 24 | +
|
| 25 | + extended('test1', { retry: 1 }, ({ file }) => { |
| 26 | + expect(file).toBe('file') |
| 27 | + }) |
| 28 | + `, |
| 29 | + }) |
| 30 | + |
| 31 | + expect(getFixtureLogs(stdout)).toMatchInlineSnapshot(` |
| 32 | + ">> init file 1 |
| 33 | + >> init file 2" |
| 34 | + `) |
| 35 | + expect(errorTree()).toMatchInlineSnapshot(` |
| 36 | + { |
| 37 | + "basic.test.ts": { |
| 38 | + "test1": "passed", |
| 39 | + }, |
| 40 | + } |
| 41 | + `) |
| 42 | +}) |
| 43 | + |
| 44 | +test('worker fixture whose setup failed does not poison the next file', async () => { |
| 45 | + const { stdout, errorTree } = await runInlineTests( |
| 46 | + { |
| 47 | + 'fixture.ts': ` |
| 48 | + import { test } from 'vitest' |
| 49 | +
|
| 50 | + let attempts = 0 |
| 51 | +
|
| 52 | + export const extended = test.extend<{ worker: string }>({ |
| 53 | + worker: [ |
| 54 | + async ({}, use) => { |
| 55 | + attempts++ |
| 56 | + console.log('>> init worker', attempts) |
| 57 | + if (attempts === 1) { |
| 58 | + throw new Error('setup failed once') |
| 59 | + } |
| 60 | + await use('worker') |
| 61 | + }, |
| 62 | + { scope: 'worker' }, |
| 63 | + ], |
| 64 | + }) |
| 65 | + `, |
| 66 | + '1-basic.test.ts': ` |
| 67 | + import { extended } from './fixture' |
| 68 | + extended('test1', ({ worker: _worker }) => {}) |
| 69 | + `, |
| 70 | + '2-basic.test.ts': ` |
| 71 | + import { extended } from './fixture' |
| 72 | + extended('test1', ({ worker: _worker }) => {}) |
| 73 | + `, |
| 74 | + }, |
| 75 | + { |
| 76 | + isolate: false, |
| 77 | + maxWorkers: 1, |
| 78 | + pool: 'threads', |
| 79 | + sequence: { sequencer: StableTestFileOrderSorter }, |
| 80 | + }, |
| 81 | + ) |
| 82 | + |
| 83 | + expect(getFixtureLogs(stdout)).toMatchInlineSnapshot(` |
| 84 | + ">> init worker 1 |
| 85 | + >> init worker 2" |
| 86 | + `) |
| 87 | + expect(errorTree()).toMatchInlineSnapshot(` |
| 88 | + { |
| 89 | + "1-basic.test.ts": { |
| 90 | + "test1": [ |
| 91 | + "setup failed once", |
| 92 | + ], |
| 93 | + }, |
| 94 | + "2-basic.test.ts": { |
| 95 | + "test1": "passed", |
| 96 | + }, |
| 97 | + } |
| 98 | + `) |
| 99 | +}) |
| 100 | + |
| 101 | +function getFixtureLogs(stdout: string) { |
| 102 | + return stdout |
| 103 | + .split('\n') |
| 104 | + .filter((line) => line.startsWith('>> init')) |
| 105 | + .join('\n') |
| 106 | +} |
| 107 | + |
| 108 | +class StableTestFileOrderSorter { |
| 109 | + sort(files: { moduleId: string }[]) { |
| 110 | + return files.sort((a, b) => a.moduleId.localeCompare(b.moduleId)) |
| 111 | + } |
| 112 | + |
| 113 | + shard(files: { moduleId: string }[]) { |
| 114 | + return files |
| 115 | + } |
| 116 | +} |
0 commit comments