Skip to content

Commit

Permalink
chore: add filesCache
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jul 2, 2022
1 parent 4f02c15 commit 6e07d44
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
23 changes: 23 additions & 0 deletions packages/vitest/src/node/cache/files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import fs, { type Stats } from 'fs'

type FileCacheStats = Pick<Stats, 'size'>

export class FilesCache {
public cache = new Map<string, FileCacheStats>()

public getStats(fsPath: string): FileCacheStats | undefined {
return this.cache.get(fsPath)
}

public async updateStats(fsPath: string) {
if (!fs.existsSync(fsPath))
return

const stats = await fs.promises.stat(fsPath)
this.cache.set(fsPath, { size: stats.size })
}

public removeStats(fsPath: string) {
this.cache.delete(fsPath)
}
}
5 changes: 5 additions & 0 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ export class Vitest {
process.exit(exitCode)
}

// populate once, update cache on watch
await Promise.all(files.map(file => this.state.stats.updateStats(file)))

await this.runFiles(files)

if (this.config.coverage.enabled)
Expand Down Expand Up @@ -361,6 +364,7 @@ export class Vitest {
if (this.state.filesMap.has(id)) {
this.state.filesMap.delete(id)
this.state.results.removeFromCache(id)
this.state.stats.removeStats(id)
this.changedTests.delete(id)
this.report('onTestRemoved', id)
}
Expand All @@ -369,6 +373,7 @@ export class Vitest {
id = slash(id)
if (await this.isTargetFile(id)) {
this.changedTests.add(id)
await this.state.stats.updateStats(id)
this.scheduleRerun(id)
}
}
Expand Down
12 changes: 10 additions & 2 deletions packages/vitest/src/node/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,16 @@ export function createPool(ctx: Vitest): WorkerPool {
const aState = ctx.state.results.getResults(a)
const bState = ctx.state.results.getResults(b)

if (!aState || !bState)
return !aState && bState ? -1 : !bState && aState ? 1 : 0
if (!aState || !bState) {
const statsA = ctx.state.stats.getStats(a)
const statsB = ctx.state.stats.getStats(b)

if (!statsA || !statsB)
return !aState && bState ? -1 : !bState && aState ? 1 : 0

// run larger files first
return statsB.size - statsA.size
}

// run failed first
if (aState.failed && !bState.failed)
Expand Down
2 changes: 2 additions & 0 deletions packages/vitest/src/node/state.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ErrorWithDiff, File, Task, TaskResultPack, UserConsoleLog } from '../types'
import { FilesCache } from './cache/files'
import { ResultsCache } from './cache/results'

export class StateManager {
Expand All @@ -7,6 +8,7 @@ export class StateManager {
taskFileMap = new WeakMap<Task, File>()
errorsSet = new Set<unknown>()
results = new ResultsCache()
stats = new FilesCache()

catchError(err: unknown, type: string) {
(err as ErrorWithDiff).type = type
Expand Down

0 comments on commit 6e07d44

Please sign in to comment.