Skip to content

Commit 9b2f944

Browse files
committed
feat: make spec into a class instead of a tuple
1 parent 1606f34 commit 9b2f944

File tree

3 files changed

+84
-21
lines changed

3 files changed

+84
-21
lines changed

packages/vitest/src/node/core.ts

+27-20
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import { WebSocketReporter } from '../api/setup'
1717
import type { SerializedCoverageConfig } from '../runtime/config'
1818
import type { SerializedSpec } from '../runtime/types/utils'
1919
import type { ArgumentsType, OnServerRestartHandler, ProvidedContext, UserConsoleLog } from '../types/general'
20-
import { createPool, getFilePoolName } from './pool'
21-
import type { ProcessPool, WorkspaceSpec } from './pool'
20+
import type { ProcessPool } from './pool'
21+
import { WorkspaceSpec, createPool, getFilePoolName } from './pool'
2222
import { createBenchmarkReporters, createReporters } from './reporters/utils'
2323
import { StateManager } from './state'
2424
import { resolveConfig } from './config/resolveConfig'
@@ -437,7 +437,7 @@ export class Vitest {
437437
}
438438
}
439439

440-
private async getTestDependencies([project, filepath]: WorkspaceSpec, deps = new Set<string>()) {
440+
private async getTestDependencies(spec: WorkspaceSpec, deps = new Set<string>()) {
441441
const addImports = async (project: WorkspaceProject, filepath: string) => {
442442
if (deps.has(filepath)) {
443443
return
@@ -459,8 +459,8 @@ export class Vitest {
459459
}))
460460
}
461461

462-
await addImports(project, filepath)
463-
deps.delete(filepath)
462+
await addImports(spec.project.workspaceProject, spec.moduleId)
463+
deps.delete(spec.moduleId)
464464

465465
return deps
466466
}
@@ -531,18 +531,18 @@ export class Vitest {
531531
for (const project of this.projects) {
532532
if (project.isTestFile(file)) {
533533
const pool = getFilePoolName(project, file)
534-
specs.push([project, file, { pool }])
534+
specs.push(new WorkspaceSpec(project, file, pool))
535535
}
536536
if (project.isTypecheckFile(file)) {
537-
specs.push([project, file, { pool: 'typescript' }])
537+
specs.push(new WorkspaceSpec(project, file, 'typescript'))
538538
}
539539
}
540540
specs.forEach(spec => this.ensureSpecCached(spec))
541541
return specs
542542
}
543543

544544
async initializeGlobalSetup(paths: WorkspaceSpec[]) {
545-
const projects = new Set(paths.map(([project]) => project))
545+
const projects = new Set(paths.map(spec => spec.project.workspaceProject))
546546
const coreProject = this.getCoreWorkspaceProject()
547547
if (!projects.has(coreProject)) {
548548
projects.add(coreProject)
@@ -566,16 +566,16 @@ export class Vitest {
566566
async runFiles(specs: WorkspaceSpec[], allTestsRun: boolean) {
567567
await this.initializeDistPath()
568568

569-
const filepaths = specs.map(([, file]) => file)
569+
const filepaths = specs.map(spec => spec.moduleId)
570570
this.state.collectPaths(filepaths)
571571

572572
await this.report('onPathsCollected', filepaths)
573573
await this.report('onSpecsCollected', specs.map(
574-
([project, file, options]) =>
574+
spec =>
575575
[{
576-
name: project.config.name,
577-
root: project.config.root,
578-
}, file, options] satisfies SerializedSpec,
576+
name: spec.project.config.name,
577+
root: spec.project.config.root,
578+
}, spec.moduleId, { pool: spec.pool }] satisfies SerializedSpec,
579579
))
580580

581581
// previous run
@@ -618,7 +618,7 @@ export class Vitest {
618618
})()
619619
.finally(async () => {
620620
// can be duplicate files if different projects are using the same file
621-
const files = Array.from(new Set(specs.map(([, p]) => p)))
621+
const files = Array.from(new Set(specs.map(spec => spec.moduleId)))
622622
const coverage = await this.coverageProvider?.generateCoverage({ allTestsRun })
623623

624624
await this.report('onFinished', this.state.getFiles(files), this.state.getUnhandledErrors(), coverage)
@@ -638,7 +638,7 @@ export class Vitest {
638638
async collectFiles(specs: WorkspaceSpec[]) {
639639
await this.initializeDistPath()
640640

641-
const filepaths = specs.map(([, file]) => file)
641+
const filepaths = specs.map(spec => spec.moduleId)
642642
this.state.collectPaths(filepaths)
643643

644644
// previous run
@@ -709,7 +709,7 @@ export class Vitest {
709709
else { this.configOverride.project = pattern }
710710

711711
this.projects = this.resolvedProjects.filter(p => p.getName() === pattern)
712-
const files = (await this.globTestFiles()).map(([, file]) => file)
712+
const files = (await this.globTestSpecs()).map(spec => spec.moduleId)
713713
await this.rerunFiles(files, 'change project filter')
714714
}
715715

@@ -1083,28 +1083,35 @@ export class Vitest {
10831083
}
10841084

10851085
public async getTestFilepaths() {
1086-
return this.globTestFiles().then(files => files.map(([, file]) => file))
1086+
return this.globTestSpecs().then(specs => specs.map(spec => spec.moduleId))
10871087
}
10881088

1089-
public async globTestFiles(filters: string[] = []) {
1089+
public async globTestSpecs(filters: string[] = []) {
10901090
const files: WorkspaceSpec[] = []
10911091
await Promise.all(this.projects.map(async (project) => {
10921092
const { testFiles, typecheckTestFiles } = await project.globTestFiles(filters)
10931093
testFiles.forEach((file) => {
10941094
const pool = getFilePoolName(project, file)
1095-
const spec: WorkspaceSpec = [project, file, { pool }]
1095+
const spec = new WorkspaceSpec(project, file, pool)
10961096
this.ensureSpecCached(spec)
10971097
files.push(spec)
10981098
})
10991099
typecheckTestFiles.forEach((file) => {
1100-
const spec: WorkspaceSpec = [project, file, { pool: 'typescript' }]
1100+
const spec = new WorkspaceSpec(project, file, 'typescript')
11011101
this.ensureSpecCached(spec)
11021102
files.push(spec)
11031103
})
11041104
}))
11051105
return files
11061106
}
11071107

1108+
/**
1109+
* @deprecated use globTestSpecs instead
1110+
*/
1111+
public async globTestFiles(filters: string[] = []) {
1112+
return this.globTestSpecs(filters)
1113+
}
1114+
11081115
private ensureSpecCached(spec: WorkspaceSpec) {
11091116
const file = spec[1]
11101117
const specs = this._cachedSpecs.get(file) || []

packages/vitest/src/node/pool.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import { createVmThreadsPool } from './pools/vmThreads'
99
import type { WorkspaceProject } from './workspace'
1010
import { createTypecheckPool } from './pools/typecheck'
1111
import { createVmForksPool } from './pools/vmForks'
12+
import { WorkspaceSpec } from './spec'
13+
14+
export { WorkspaceSpec }
1215

13-
export type WorkspaceSpec = [project: WorkspaceProject, testFile: string, options: { pool: Pool }]
1416
export type RunWithFiles = (
1517
files: WorkspaceSpec[],
1618
invalidates?: string[]

packages/vitest/src/node/spec.ts

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import type { TestProject } from './reported-workspace-project'
2+
import type { Pool } from './types/pool-options'
3+
import type { WorkspaceProject } from './workspace'
4+
5+
export class WorkspaceSpec {
6+
// backwards compatibility
7+
/**
8+
* @deprecated
9+
*/
10+
public readonly 0: WorkspaceProject
11+
/**
12+
* @deprecated
13+
*/
14+
public readonly 1: string
15+
/**
16+
* @deprecated
17+
*/
18+
public readonly 2: { pool: Pool }
19+
20+
public readonly project: TestProject
21+
public readonly moduleId: string
22+
public readonly pool: Pool
23+
public readonly location: WorkspaceSpecLocation | undefined
24+
25+
constructor(
26+
workspaceProject: WorkspaceProject,
27+
moduleId: string,
28+
pool: Pool,
29+
location?: WorkspaceSpecLocation | undefined,
30+
) {
31+
this[0] = workspaceProject
32+
this[1] = moduleId
33+
this[2] = { pool }
34+
this.project = workspaceProject.testProject
35+
this.moduleId = moduleId
36+
this.pool = pool
37+
this.location = location
38+
}
39+
40+
/**
41+
* for backwards compatibility
42+
* @deprecated
43+
*/
44+
*[Symbol.iterator]() {
45+
yield this.project.workspaceProject
46+
yield this.moduleId
47+
yield this.pool
48+
}
49+
}
50+
51+
interface WorkspaceSpecLocation {
52+
start: number
53+
end: number
54+
}

0 commit comments

Comments
 (0)