-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Addon Test: Always run Vitest in watch mode internally #29749
Changes from all commits
86e4c96
92ef7bf
e5fb87d
47101c6
50bfe4a
685a3e8
57cb78b
12179c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,20 +39,17 @@ export class TestManager { | |
this.vitestManager.startVitest().then(() => options.onReady?.()); | ||
} | ||
|
||
async handleConfigChange( | ||
payload: TestingModuleConfigChangePayload<{ coverage: boolean; a11y: boolean }> | ||
) { | ||
async handleConfigChange(payload: TestingModuleConfigChangePayload<Config>) { | ||
if (payload.providerId !== TEST_PROVIDER_ID) { | ||
return; | ||
} | ||
|
||
process.env.VITEST_STORYBOOK_CONFIG = JSON.stringify(payload.config); | ||
|
||
if (this.coverage !== payload.config.coverage) { | ||
this.coverage = payload.config.coverage; | ||
try { | ||
this.coverage = payload.config.coverage; | ||
await this.vitestManager.restartVitest({ | ||
watchMode: this.watchMode, | ||
coverage: this.coverage, | ||
}); | ||
} catch (e) { | ||
|
@@ -68,25 +65,31 @@ export class TestManager { | |
} | ||
} | ||
|
||
async handleWatchModeRequest(payload: TestingModuleWatchModeRequestPayload) { | ||
try { | ||
if (payload.providerId !== TEST_PROVIDER_ID) { | ||
return; | ||
} | ||
async handleWatchModeRequest(payload: TestingModuleWatchModeRequestPayload<Config>) { | ||
if (payload.providerId !== TEST_PROVIDER_ID) { | ||
return; | ||
} | ||
this.watchMode = payload.watchMode; | ||
|
||
if (payload.config) { | ||
this.handleConfigChange({ | ||
providerId: payload.providerId, | ||
config: payload.config as any, | ||
}); | ||
} | ||
if (payload.config) { | ||
this.handleConfigChange({ | ||
providerId: payload.providerId, | ||
config: payload.config, | ||
}); | ||
} | ||
|
||
if (this.watchMode !== payload.watchMode) { | ||
this.watchMode = payload.watchMode; | ||
await this.vitestManager.restartVitest({ watchMode: this.watchMode, coverage: false }); | ||
if (this.coverage) { | ||
try { | ||
if (payload.watchMode) { | ||
// if watch mode is toggled on and coverage is already enabled, restart vitest without coverage to automatically disable it | ||
await this.vitestManager.restartVitest({ coverage: false }); | ||
} else { | ||
// if watch mode is toggled off and coverage is already enabled, restart vitest with coverage to automatically re-enable it | ||
await this.vitestManager.restartVitest({ coverage: this.coverage }); | ||
} | ||
} catch (e) { | ||
this.reportFatalError('Failed to change watch mode while coverage was enabled', e); | ||
} | ||
} catch (e) { | ||
this.reportFatalError('Failed to change watch mode', e); | ||
} | ||
} | ||
|
||
|
@@ -96,38 +99,32 @@ export class TestManager { | |
return; | ||
} | ||
|
||
const allTestsRun = (payload.storyIds ?? []).length === 0; | ||
|
||
if (payload.config && this.coverage !== payload.config.coverage) { | ||
this.coverage = payload.config.coverage; | ||
if (payload.config) { | ||
this.handleConfigChange({ | ||
providerId: payload.providerId, | ||
config: payload.config, | ||
}); | ||
} | ||
Comment on lines
+102
to
107
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: handleConfigChange is called before checking if the config actually changed, which could trigger unnecessary restarts |
||
|
||
if (this.coverage) { | ||
/* | ||
If we have coverage enabled and we're running all stories, | ||
we have to restart Vitest AND disable watch mode otherwise the coverage report will be incorrect, | ||
Vitest behaves wonky when re-using the same Vitest instance but with watch mode disabled, | ||
among other things it causes the coverage report to be incorrect and stale. | ||
|
||
If we're only running a subset of stories, we have to temporarily disable coverage, | ||
as a coverage report for a subset of stories is not useful. | ||
*/ | ||
/* | ||
If we're only running a subset of stories, we have to temporarily disable coverage, | ||
as a coverage report for a subset of stories is not useful. | ||
*/ | ||
const temporarilyDisableCoverage = | ||
this.coverage && !this.watchMode && (payload.storyIds ?? []).length > 0; | ||
if (temporarilyDisableCoverage) { | ||
await this.vitestManager.restartVitest({ | ||
watchMode: allTestsRun ? false : this.watchMode, | ||
coverage: allTestsRun, | ||
coverage: false, | ||
}); | ||
} else { | ||
await this.vitestManager.vitestRestartPromise; | ||
} | ||
|
||
await this.vitestManager.runTests(payload); | ||
|
||
if (this.coverage && !allTestsRun) { | ||
if (temporarilyDisableCoverage) { | ||
// Re-enable coverage if it was temporarily disabled because of a subset of stories was run | ||
await this.vitestManager.restartVitest({ | ||
watchMode: this.watchMode, | ||
coverage: this.coverage, | ||
}); | ||
await this.vitestManager.restartVitest({ coverage: this.coverage }); | ||
} | ||
} catch (e) { | ||
this.reportFatalError('Failed to run tests', e); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,7 +40,7 @@ export class VitestManager { | |
|
||
constructor(private testManager: TestManager) {} | ||
|
||
async startVitest({ watchMode = false, coverage = false } = {}) { | ||
async startVitest({ coverage = false } = {}) { | ||
const { createVitest } = await import('vitest/node'); | ||
|
||
const storybookCoverageReporter: [string, StorybookCoverageReporterOptions] = [ | ||
|
@@ -55,7 +55,7 @@ export class VitestManager { | |
? { | ||
enabled: true, | ||
clean: false, | ||
cleanOnRerun: !watchMode, | ||
cleanOnRerun: false, | ||
Comment on lines
57
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: cleanOnRerun:false could potentially lead to stale coverage data if files are deleted or renamed |
||
reportOnFailure: true, | ||
reporter: [['html', {}], storybookCoverageReporter], | ||
reportsDirectory: resolvePathInStorybookCache(COVERAGE_DIRECTORY), | ||
|
@@ -66,9 +66,8 @@ export class VitestManager { | |
this.vitest = await createVitest( | ||
'test', | ||
{ | ||
watch: watchMode, | ||
watch: true, | ||
passWithNoTests: false, | ||
changed: watchMode, | ||
// TODO: | ||
// Do we want to enable Vite's default reporter? | ||
// The output in the terminal might be too spamy and it might be better to | ||
|
@@ -110,18 +109,16 @@ export class VitestManager { | |
this.testManager.reportFatalError('Failed to init Vitest', e); | ||
} | ||
|
||
if (watchMode) { | ||
await this.setupWatchers(); | ||
} | ||
await this.setupWatchers(); | ||
} | ||
|
||
async restartVitest({ watchMode, coverage }: { watchMode: boolean; coverage: boolean }) { | ||
async restartVitest({ coverage }: { coverage: boolean }) { | ||
await this.vitestRestartPromise; | ||
this.vitestRestartPromise = new Promise(async (resolve, reject) => { | ||
try { | ||
await this.vitest?.runningPromise; | ||
await this.closeVitest(); | ||
await this.startVitest({ watchMode, coverage }); | ||
await this.startVitest({ coverage }); | ||
resolve(); | ||
} catch (e) { | ||
reject(e); | ||
|
@@ -324,6 +321,11 @@ export class VitestManager { | |
this.updateLastChanged(id); | ||
this.storyCountForCurrentRun = 0; | ||
|
||
// when watch mode is disabled, don't trigger any tests (below) | ||
// but still invalidate the cache for the changed file, which is handled above | ||
if (!this.testManager.watchMode) { | ||
return; | ||
} | ||
JReinhold marked this conversation as resolved.
Show resolved
Hide resolved
|
||
await this.runAffectedTests(file); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: test expects 4 createVitest calls but only verifies empty test run. Should verify both coverage-disabled and coverage-enabled runs