Skip to content

Commit

Permalink
feat: add clearSpecificationsCache
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Dec 3, 2024
1 parent afc6619 commit d7ffdbc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
4 changes: 2 additions & 2 deletions packages/vitest/src/node/cli/cac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ async function start(mode: VitestRunMode, cliFilters: string[], options: CliOpti
try {
const { startVitest } = await import('./cli-api')
const ctx = await startVitest(mode, cliFilters.map(normalize), normalizeCliOptions(options))
if (!ctx?.shouldKeepServer()) {
await ctx?.exit()
if (!ctx.shouldKeepServer()) {
await ctx.exit()
}
}
catch (e) {
Expand Down
20 changes: 14 additions & 6 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,12 +614,20 @@ export class Vitest {
return this.specifications.getModuleSpecifications(moduleId)
}

/**
* Vitest automatically caches test specifications for each file. This method clears the cache for the given file or the whole cache alltogether.
*/
public clearSpecificationsCache(moduleId?: string) {
this.specifications.clearCache(moduleId)
}

/**
* Run tests for the given test specifications. This does not trigger `onWatcher*` events.
* @param specifications A list of specifications to run.
* @param allTestsRun Indicates whether all tests were run. This only matters for coverage.
*/
public runTestSpecifications(specifications: TestSpecification[], allTestsRun = false): Promise<TestRunResult> {
specifications.forEach(spec => this.specifications.ensureSpecificationCached(spec))
return this.runFiles(specifications, allTestsRun)
}

Expand Down Expand Up @@ -1070,7 +1078,7 @@ export class Vitest {
this.state.getProcessTimeoutCauses().forEach(cause => console.warn(cause))

if (!this.pool) {
const runningServers = [this.server, ...this.resolvedProjects.map(p => p.server)].filter(Boolean).length
const runningServers = [this.vite, ...this.resolvedProjects.map(p => p.vite)].filter(Boolean).length

if (runningServers === 1) {
console.warn('Tests closed successfully but something prevents Vite server from exiting')
Expand Down Expand Up @@ -1126,28 +1134,28 @@ export class Vitest {
/**
* Should the server be kept running after the tests are done.
*/
shouldKeepServer() {
shouldKeepServer(): boolean {
return !!this.config?.watch
}

/**
* Register a handler that will be called when the server is restarted due to a config change.
*/
onServerRestart(fn: OnServerRestartHandler) {
onServerRestart(fn: OnServerRestartHandler): void {
this._onRestartListeners.push(fn)
}

/**
* Register a handler that will be called when the test run is cancelled with `vitest.cancelCurrentRun`.
*/
onCancel(fn: (reason: CancelReason) => Awaitable<void>) {
onCancel(fn: (reason: CancelReason) => Awaitable<void>): void {
this._onCancelListeners.push(fn)
}

/**
* Register a handler that will be called when the server is closed.
*/
onClose(fn: () => Awaitable<void>) {
onClose(fn: () => Awaitable<void>): void {
this._onClose.push(fn)
}

Expand All @@ -1170,7 +1178,7 @@ export class Vitest {
}

/** @internal */
onAfterSetServer(fn: OnServerRestartHandler) {
onAfterSetServer(fn: OnServerRestartHandler): void {
this._onSetServer.push(fn)
}
}
Expand Down
18 changes: 13 additions & 5 deletions packages/vitest/src/node/specifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,30 @@ export class VitestSpecifications {
return files
}

public clearCache(): void {
this._cachedSpecs.clear()
public clearCache(moduleId?: string): void {
if (moduleId) {
this._cachedSpecs.delete(moduleId)
}
else {
this._cachedSpecs.clear()
}
}

private getCachedSpecifications(moduleId: string): TestSpecification[] | undefined {
return this._cachedSpecs.get(moduleId)
}

private ensureSpecificationCached(spec: TestSpecification): TestSpecification[] {
public ensureSpecificationCached(spec: TestSpecification): TestSpecification[] {
const file = spec.moduleId
const specs = this._cachedSpecs.get(file) || []
const included = specs.some(_s => _s.project === spec.project && _s.pool === spec.pool)
if (!included) {
const index = specs.findIndex(_s => _s.project === spec.project && _s.pool === spec.pool)
if (index === -1) {
specs.push(spec)
this._cachedSpecs.set(file, specs)
}
else {
specs.splice(index, 1, spec)
}
return specs
}

Expand Down

0 comments on commit d7ffdbc

Please sign in to comment.