Skip to content

Commit

Permalink
fix(browser): wait for files scanning in browser (#1704)
Browse files Browse the repository at this point in the history
* fix: wait for files scanning

* fix: update

* fix: update types and make them Promise
  • Loading branch information
Aslemammad committed Jul 26, 2022
1 parent 7b0e4c9 commit d4e6612
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/vitest/src/api/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ export function setup(ctx: Vitest) {
getFiles() {
return ctx.state.getFiles()
},
getPaths() {
return ctx.state.getPaths()
async getPaths() {
return await ctx.state.getPaths()
},
readFile(id) {
return fs.readFile(id, 'utf-8')
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface WebSocketHandlers {
onCollected(files?: File[]): Promise<void>
onTaskUpdate(packs: TaskResultPack[]): void
getFiles(): File[]
getPaths(): string[]
getPaths(): Promise<string[]>
getConfig(): ResolvedConfig
getModuleGraph(id: string): Promise<ModuleGraphData>
getTransformResult(id: string): Promise<TransformResultWithSource | undefined>
Expand Down
2 changes: 2 additions & 0 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ export class Vitest {
async runFiles(paths: string[]) {
// previous run
await this.runningPromise
this.state.startCollectingPaths()

// schedule the new run
this.runningPromise = (async () => {
Expand Down Expand Up @@ -243,6 +244,7 @@ export class Vitest {
})()
.finally(() => {
this.runningPromise = undefined
this.state.finishCollectingPaths()
})

return await this.runningPromise
Expand Down
22 changes: 21 additions & 1 deletion packages/vitest/src/node/state.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import type { ErrorWithDiff, File, Task, TaskResultPack, UserConsoleLog } from '../types'

interface CollectingPromise {
promise: Promise<void>
resolve: () => void
}

// Note this file is shared for both node and browser, be aware to avoid node specific logic
export class StateManager {
filesMap = new Map<string, File>()
pathsSet: Set<string> = new Set()
collectingPromise: CollectingPromise | undefined = undefined
idMap = new Map<string, Task>()
taskFileMap = new WeakMap<Task, File>()
errorsSet = new Set<unknown>()
Expand All @@ -21,7 +27,21 @@ export class StateManager {
return Array.from(this.errorsSet.values())
}

getPaths() {
startCollectingPaths() {
let _resolve: CollectingPromise['resolve']
const promise = new Promise<void>((resolve) => {
_resolve = resolve
})
this.collectingPromise = { promise, resolve: _resolve! }
}

finishCollectingPaths() {
this.collectingPromise?.resolve()
this.collectingPromise = undefined
}

async getPaths() {
await this.collectingPromise?.promise
return Array.from(this.pathsSet)
}

Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d4e6612

Please sign in to comment.