Skip to content

Commit

Permalink
fix: correctly resolve config in a workspace (#3947)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Aug 15, 2023
1 parent e73ca9e commit 1c08d5d
Show file tree
Hide file tree
Showing 18 changed files with 273 additions and 114 deletions.
3 changes: 2 additions & 1 deletion packages/browser/src/client/runner.ts
Expand Up @@ -42,7 +42,8 @@ export function createBrowserRunner(original: any, coverageModule: CoverageHandl
async onAfterRun() {
await super.onAfterRun?.()
const coverage = await coverageModule?.takeCoverage?.()
await rpc().onAfterSuiteRun({ coverage })
if (coverage)
await rpc().onAfterSuiteRun({ coverage })
}

onCollected(files: File[]): unknown {
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/api/setup.ts
Expand Up @@ -92,7 +92,7 @@ export function setup(vitestOrWorkspace: Vitest | WorkspaceProject, server?: Vit
await ctx.rerunFiles(files)
},
getConfig() {
return ctx.config
return vitestOrWorkspace.config
},
async getTransformResult(id) {
const result: TransformResultWithSource | null | undefined = await ctx.vitenode.transformRequest(id)
Expand Down
28 changes: 22 additions & 6 deletions packages/vitest/src/node/core.ts
Expand Up @@ -53,7 +53,7 @@ export class Vitest {
restartsCount = 0
runner: ViteNodeRunner = undefined!

private coreWorkspace!: WorkspaceProject
private coreWorkspaceProject!: WorkspaceProject

public projects: WorkspaceProject[] = []
private projectsTestFiles = new Map<string, Set<WorkspaceProject>>()
Expand Down Expand Up @@ -145,12 +145,12 @@ export class Vitest {
runner: this.runner,
server: this.vitenode,
})
this.coreWorkspace = coreWorkspace
this.coreWorkspaceProject = coreWorkspace
return coreWorkspace
}

public getCoreWorkspaceProject(): WorkspaceProject | null {
return this.coreWorkspace || null
return this.coreWorkspaceProject || null
}

public getProjectByTaskId(taskId: string): WorkspaceProject {
Expand Down Expand Up @@ -198,7 +198,7 @@ export class Vitest {
onlyFiles: false,
markDirectories: true,
cwd: this.config.root,
ignore: ['**/node_modules/**'],
ignore: ['**/node_modules/**', '**/*.timestamp-*'],
}

const workspacesFs = await fg(workspaceGlobMatches, globOptions)
Expand All @@ -221,6 +221,21 @@ export class Vitest {
return filepath
}))

const workspacesByFolder = resolvedWorkspacesPaths
.reduce((configByFolder, filepath) => {
const dir = dirname(filepath)
configByFolder[dir] ??= []
configByFolder[dir].push(filepath)
return configByFolder
}, {} as Record<string, string[]>)

const filteredWorkspaces = Object.values(workspacesByFolder).map((configFiles) => {
if (configFiles.length === 1)
return configFiles[0]
const vitestConfig = configFiles.find(configFile => basename(configFile).startsWith('vitest.config'))
return vitestConfig || configFiles[0]
})

const overridesOptions = [
'logHeapUsage',
'allowOnly',
Expand All @@ -239,7 +254,7 @@ export class Vitest {
return acc
}, {} as UserConfig)

const projects = resolvedWorkspacesPaths.map(async (workspacePath) => {
const projects = filteredWorkspaces.map(async (workspacePath) => {
// don't start a new server, but reuse existing one
if (
this.server.config.configFile === workspacePath
Expand Down Expand Up @@ -725,7 +740,8 @@ export class Vitest {
if (!this.closingPromise) {
const closePromises = this.projects.map(w => w.close().then(() => w.server = undefined as any))
// close the core workspace server only once
if (this.coreWorkspace && !this.projects.includes(this.coreWorkspace))
// it's possible that it's not initialized at all because it's not running any tests
if (!this.coreWorkspaceProject || !this.projects.includes(this.coreWorkspaceProject))
closePromises.push(this.server.close().then(() => this.server = undefined as any))

if (this.pool)
Expand Down

0 comments on commit 1c08d5d

Please sign in to comment.