Skip to content
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

fix(browser): wait for files scanning in browser #1704

Merged
merged 3 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
22 changes: 18 additions & 4 deletions pnpm-lock.yaml

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