-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat(vitest): add onTestsRerun method to global setup context
#6803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sheremet-va
merged 4 commits into
vitest-dev:main
from
sheremet-va:feat/onwatcher-rerun
Nov 13, 2024
+104
−6
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
19cfa88
feat(vitest): add `onWatcherRerun` method to global setup context
sheremet-va fd36f67
test: add test for the global setup
sheremet-va a34b4f5
test: add a test with a manual rerun
sheremet-va 510e64a
refactor: rename onWatcherRerun to onTestsRerun
sheremet-va File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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']) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.