-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vitest): add
onTestsRerun
method to global setup context (#6803)
- Loading branch information
1 parent
9c8f7e3
commit e26e066
Showing
9 changed files
with
104 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { GlobalSetupContext } from 'vitest/node'; | ||
|
||
const calls: string[] = []; | ||
|
||
(globalThis as any).__CALLS = calls | ||
|
||
export default ({ onTestsRerun }: GlobalSetupContext) => { | ||
calls.push('start') | ||
onTestsRerun(() => { | ||
calls.push('rerun') | ||
}) | ||
return () => { | ||
calls.push('end') | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { expect, test } from 'vitest' | ||
import { editFile, runVitest } from '../../test-utils' | ||
|
||
const testFile = 'fixtures/math.test.ts' | ||
|
||
test('global setup calls hooks correctly when file changes', async () => { | ||
process.env.TEST_GLOBAL_SETUP = 'true' | ||
const { vitest, ctx } = await runVitest({ | ||
root: 'fixtures', | ||
watch: true, | ||
include: ['math.test.ts'], | ||
}) | ||
|
||
await vitest.waitForStdout('Waiting for file changes') | ||
|
||
const calls = (globalThis as any).__CALLS as string[] | ||
expect(calls).toEqual(['start']) | ||
|
||
editFile(testFile, testFileContent => `${testFileContent}\n\n`) | ||
|
||
await vitest.waitForStdout('RERUN') | ||
expect(calls).toEqual(['start', 'rerun']) | ||
|
||
await ctx?.close() | ||
|
||
expect(calls).toEqual(['start', 'rerun', 'end']) | ||
}) | ||
|
||
test('global setup calls hooks correctly with a manual rerun', async () => { | ||
process.env.TEST_GLOBAL_SETUP = 'true' | ||
const { vitest, ctx } = await runVitest({ | ||
root: 'fixtures', | ||
watch: true, | ||
include: ['math.test.ts'], | ||
}) | ||
|
||
await vitest.waitForStdout('Waiting for file changes') | ||
|
||
const calls = (globalThis as any).__CALLS as string[] | ||
expect(calls).toEqual(['start']) | ||
|
||
vitest.write('r') | ||
|
||
await vitest.waitForStdout('RERUN') | ||
expect(calls).toEqual(['start', 'rerun']) | ||
|
||
await ctx?.close() | ||
|
||
expect(calls).toEqual(['start', 'rerun', 'end']) | ||
}) |