Skip to content

Commit

Permalink
Merge branch 'main' into perf-use-performance-now-instead-of-date-now
Browse files Browse the repository at this point in the history
  • Loading branch information
LuciNyan authored Aug 26, 2024
2 parents 0cc22f2 + 9f78a91 commit 6b6099c
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 61 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 @@ -12,7 +12,7 @@ import type { Awaitable, ModuleGraphData, UserConsoleLog } from '../types/genera
import type { Reporter } from '../node/types/reporter'
import { getModuleGraph, isPrimitive, noop, stringifyReplace } from '../utils'
import { parseErrorStacktrace } from '../utils/source-map'
import type { SerializedSpec } from '../runtime/types/utils'
import type { SerializedTestSpecification } from '../runtime/types/utils'
import type {
TransformResultWithSource,
WebSocketEvents,
Expand Down Expand Up @@ -157,7 +157,7 @@ export class WebSocketReporter implements Reporter {
})
}

onSpecsCollected(specs?: SerializedSpec[] | undefined): Awaitable<void> {
onSpecsCollected(specs?: SerializedTestSpecification[] | undefined): Awaitable<void> {
if (this.clients.size === 0) {
return
}
Expand Down
6 changes: 3 additions & 3 deletions packages/vitest/src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { BirpcReturn } from 'birpc'
import type { File, TaskResultPack } from '@vitest/runner'
import type { Awaitable, ModuleGraphData, UserConsoleLog } from '../types/general'
import type { SerializedConfig } from '../runtime/config'
import type { SerializedSpec } from '../runtime/types/utils'
import type { SerializedTestSpecification } from '../runtime/types/utils'

interface SourceMap {
file: string
Expand All @@ -29,7 +29,7 @@ export interface TransformResultWithSource {
export interface WebSocketHandlers {
onTaskUpdate: (packs: TaskResultPack[]) => void
getFiles: () => File[]
getTestFiles: () => Promise<SerializedSpec[]>
getTestFiles: () => Promise<SerializedTestSpecification[]>
getPaths: () => string[]
getConfig: () => SerializedConfig
getModuleGraph: (
Expand Down Expand Up @@ -59,7 +59,7 @@ export interface WebSocketEvents {
onTaskUpdate?: (packs: TaskResultPack[]) => Awaitable<void>
onUserConsoleLog?: (log: UserConsoleLog) => Awaitable<void>
onPathsCollected?: (paths?: string[]) => Awaitable<void>
onSpecsCollected?: (specs?: SerializedSpec[]) => Awaitable<void>
onSpecsCollected?: (specs?: SerializedTestSpecification[]) => Awaitable<void>
onFinishedReportCoverage: () => void
}

Expand Down
18 changes: 6 additions & 12 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { workspacesFiles as workspaceFiles } from '../constants'
import { rootDir } from '../paths'
import { WebSocketReporter } from '../api/setup'
import type { SerializedCoverageConfig } from '../runtime/config'
import type { SerializedSpec } from '../runtime/types/utils'
import type { ArgumentsType, OnServerRestartHandler, ProvidedContext, UserConsoleLog } from '../types/general'
import type { ProcessPool, WorkspaceSpec } from './pool'
import { createPool, getFilePoolName } from './pool'
Expand All @@ -32,6 +31,7 @@ import type { ResolvedConfig, UserConfig, VitestRunMode } from './types/config'
import type { Reporter } from './types/reporter'
import type { CoverageProvider } from './types/coverage'
import { resolveWorkspace } from './workspace/resolveWorkspace'
import type { TestSpecification } from './spec'

const WATCHER_DEBOUNCE = 100

Expand Down Expand Up @@ -299,7 +299,7 @@ export class Vitest {
throw new Error('Cannot merge reports when `--reporter=blob` is used. Remove blob reporter from the config first.')
}

const { files, errors, coverages } = await readBlobs(this.config.mergeReports, this.projects)
const { files, errors, coverages } = await readBlobs(this.version, this.config.mergeReports, this.projects)

await this.report('onInit', this)
await this.report('onPathsCollected', files.flatMap(f => f.filepath))
Expand Down Expand Up @@ -541,7 +541,7 @@ export class Vitest {
return specs
}

async initializeGlobalSetup(paths: WorkspaceSpec[]) {
async initializeGlobalSetup(paths: TestSpecification[]) {
const projects = new Set(paths.map(spec => spec.project.workspaceProject))
const coreProject = this.getCoreWorkspaceProject()
if (!projects.has(coreProject)) {
Expand All @@ -563,20 +563,14 @@ export class Vitest {
this.distPath = join(vitestDir, 'dist')
}

async runFiles(specs: WorkspaceSpec[], allTestsRun: boolean) {
async runFiles(specs: TestSpecification[], allTestsRun: boolean) {
await this.initializeDistPath()

const filepaths = specs.map(spec => spec.moduleId)
this.state.collectPaths(filepaths)

await this.report('onPathsCollected', filepaths)
await this.report('onSpecsCollected', specs.map(
spec =>
[{
name: spec.project.config.name,
root: spec.project.config.root,
}, spec.moduleId, { pool: spec.pool }] satisfies SerializedSpec,
))
await this.report('onSpecsCollected', specs.map(spec => spec.toJSON()))

// previous run
await this.runningPromise
Expand All @@ -601,7 +595,7 @@ export class Vitest {
await this.initializeGlobalSetup(specs)

try {
await this.pool.runTests(specs, invalidates)
await this.pool.runTests(specs as WorkspaceSpec[], invalidates)
}
catch (err) {
this.state.catchError(err, 'Unhandled Error')
Expand Down
7 changes: 5 additions & 2 deletions packages/vitest/src/node/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ import { createVmThreadsPool } from './pools/vmThreads'
import type { WorkspaceProject } from './workspace'
import { createTypecheckPool } from './pools/typecheck'
import { createVmForksPool } from './pools/vmForks'
import type { WorkspaceSpec as _WorkspaceSpec } from './spec'
import type { TestSpecification } from './spec'

export type WorkspaceSpec = _WorkspaceSpec & [
/**
* @deprecated use TestSpecification instead
*/
export type WorkspaceSpec = TestSpecification & [
/**
* @deprecated use spec.project instead
*/
Expand Down
57 changes: 31 additions & 26 deletions packages/vitest/src/node/reporters/blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { mkdir, readFile, readdir, stat, writeFile } from 'node:fs/promises'
import { existsSync } from 'node:fs'
import { parse, stringify } from 'flatted'
import { dirname, resolve } from 'pathe'
import { cleanUrl } from 'vite-node/utils'
import type { File } from '@vitest/runner'
import { getOutputFile } from '../../utils/config-helpers'
import type { WorkspaceProject } from '../workspace'
Expand Down Expand Up @@ -43,11 +42,16 @@ export class BlobReporter implements Reporter {
: '.vitest-reports/blob.json'
}

const moduleKeys = this.ctx.projects.map<MergeReportModuleKeys>(
const modules = this.ctx.projects.map<MergeReportModuleKeys>(
(project) => {
return [
project.getName(),
[...project.server.moduleGraph.idToModuleMap.keys()],
[...project.server.moduleGraph.idToModuleMap.entries()].map<SerializedModuleNode | null>((mod) => {
if (!mod[1].file) {
return null
}
return [mod[0], mod[1].file, mod[1].url]
}).filter(x => x != null),
]
},
)
Expand All @@ -56,7 +60,7 @@ export class BlobReporter implements Reporter {
this.ctx.version,
files,
errors,
moduleKeys,
modules,
coverage,
] satisfies MergeReport)

Expand All @@ -73,6 +77,7 @@ export class BlobReporter implements Reporter {
}

export async function readBlobs(
currentVersion: string,
blobsDirectory: string,
projectsArray: WorkspaceProject[],
) {
Expand Down Expand Up @@ -113,6 +118,12 @@ export async function readBlobs(
)
}

if (!versions.has(currentVersion)) {
throw new Error(
`the blobs in "${blobsDirectory}" were generated by a different version of Vitest. Expected v${currentVersion}, but received v${blobs[0].version}`,
)
}

// fake module graph - it is used to check if module is imported, but we don't use values inside
const projects = Object.fromEntries(
projectsArray.map(p => [p.getName(), p]),
Expand All @@ -124,26 +135,11 @@ export async function readBlobs(
if (!project) {
return
}
moduleIds.forEach((moduleId) => {
project.server.moduleGraph.idToModuleMap.set(moduleId, {
id: moduleId,
url: moduleId,
file: cleanUrl(moduleId),
ssrTransformResult: null,
transformResult: null,
importedBindings: null,
importedModules: new Set(),
importers: new Set(),
type: 'js',
clientImportedModules: new Set(),
ssrError: null,
ssrImportedModules: new Set(),
ssrModule: null,
acceptedHmrDeps: new Set(),
acceptedHmrExports: null,
lastHMRTimestamp: 0,
lastInvalidationTimestamp: 0,
})
moduleIds.forEach(([moduleId, file, url]) => {
const moduleNode = project.server.moduleGraph.createFileOnlyEntry(file)
moduleNode.url = url
moduleNode.id = moduleId
project.server.moduleGraph.idToModuleMap.set(moduleId, moduleNode)
})
})
})
Expand All @@ -169,8 +165,17 @@ type MergeReport = [
vitestVersion: string,
files: File[],
errors: unknown[],
moduleKeys: MergeReportModuleKeys[],
modules: MergeReportModuleKeys[],
coverage: unknown,
]

type MergeReportModuleKeys = [projectName: string, moduleIds: string[]]
type SerializedModuleNode = [
id: string,
file: string,
url: string,
]

type MergeReportModuleKeys = [
projectName: string,
modules: SerializedModuleNode[],
]
21 changes: 16 additions & 5 deletions packages/vitest/src/node/spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import type { SerializedTestSpecification } from '../runtime/types/utils'
import type { TestProject } from './reported-workspace-project'
import type { Pool } from './types/pool-options'
import type { WorkspaceProject } from './workspace'

export class WorkspaceSpec {
// backwards compatibility
export class TestSpecification {
/**
* @deprecated
* @deprecated use `project` instead
*/
public readonly 0: WorkspaceProject
/**
* @deprecated
* @deprecated use `moduleId` instead
*/
public readonly 1: string
/**
* @deprecated
* @deprecated use `pool` instead
*/
public readonly 2: { pool: Pool }

Expand All @@ -37,6 +37,17 @@ export class WorkspaceSpec {
// this.location = location
}

toJSON(): SerializedTestSpecification {
return [
{
name: this.project.config.name,
root: this.project.config.root,
},
this.moduleId,
{ pool: this.pool },
]
}

/**
* for backwards compatibility
* @deprecated
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/node/types/reporter.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { File, TaskResultPack } from '@vitest/runner'
import type { Vitest } from '../core'
import type { SerializedSpec } from '../../runtime/types/utils'
import type { SerializedTestSpecification } from '../../runtime/types/utils'
import type { Awaitable, UserConsoleLog } from '../../types/general'

export interface Reporter {
onInit?: (ctx: Vitest) => void
onPathsCollected?: (paths?: string[]) => Awaitable<void>
onSpecsCollected?: (specs?: SerializedSpec[]) => Awaitable<void>
onSpecsCollected?: (specs?: SerializedTestSpecification[]) => Awaitable<void>
onCollected?: (files?: File[]) => Awaitable<void>
onFinished?: (
files: File[],
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/node/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { CoverageTransform } from './plugins/coverageTransform'
import { serializeConfig } from './config/serializeConfig'
import type { Vitest } from './core'
import { TestProject } from './reported-workspace-project'
import { WorkspaceSpec } from './spec'
import { TestSpecification } from './spec'
import type { WorkspaceSpec as DeprecatedWorkspaceSpec } from './pool'

interface InitializeProjectOptions extends UserWorkspaceConfig {
Expand Down Expand Up @@ -154,7 +154,7 @@ export class WorkspaceProject {
}

public createSpec(moduleId: string, pool: string): DeprecatedWorkspaceSpec {
return new WorkspaceSpec(this, moduleId, pool) as DeprecatedWorkspaceSpec
return new TestSpecification(this, moduleId, pool) as DeprecatedWorkspaceSpec
}

async initializeGlobalSetup() {
Expand Down
8 changes: 4 additions & 4 deletions packages/vitest/src/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ import type {
BenchmarkUserOptions as BenchmarkUserOptions_,
} from '../node/types/benchmark'

import type { SerializedSpec } from '../runtime/types/utils'
import type { SerializedTestSpecification } from '../runtime/types/utils'

export {
suite,
Expand Down Expand Up @@ -339,9 +339,9 @@ export type {
BenchTask,
} from '../runtime/types/benchmark'

/** @deprecated use `SerializedSpec` instead */
export type SerializableSpec = SerializedSpec
export type { SerializedSpec }
/** @deprecated use `SerializedTestSpecification` instead */
export type SerializableSpec = SerializedTestSpecification
export type { SerializedTestSpecification }

/** @deprecated import from `vitest/node` instead */
export type BenchmarkUserOptions = BenchmarkUserOptions_
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/public/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export { VitestPackageInstaller } from '../node/packageInstaller'
export { createDebugger } from '../utils/debugger'
export { resolveFsAllow } from '../node/plugins/utils'
export { resolveApiServerConfig, resolveConfig } from '../node/config/resolveConfig'
export { TestSpecification } from '../node/spec'

export { GitNotFoundError, FilesNotFoundError as TestsNotFoundError } from '../node/errors'

Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/runtime/types/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type SerializedSpec = [
export type SerializedTestSpecification = [
project: { name: string | undefined; root: string },
file: string,
options: { pool: string },
Expand Down
4 changes: 2 additions & 2 deletions test/core/test/sequencers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Vitest, WorkspaceProject } from 'vitest/node'
import { describe, expect, test, vi } from 'vitest'
import { RandomSequencer } from '../../../packages/vitest/src/node/sequencers/RandomSequencer'
import { BaseSequencer } from '../../../packages/vitest/src/node/sequencers/BaseSequencer'
import { WorkspaceSpec } from '../../../packages/vitest/src/node/spec'
import { TestSpecification } from '../../../packages/vitest/src/node/spec'
import type { WorkspaceSpec as DeprecatedWorkspaceSpec } from '../../../packages/vitest/src/node/pool'

function buildCtx() {
Expand All @@ -28,7 +28,7 @@ function buildWorkspace() {
const workspace = buildWorkspace()

function workspaced(files: string[]) {
return files.map(file => new WorkspaceSpec(workspace, file, 'forks')) as DeprecatedWorkspaceSpec[]
return files.map(file => new TestSpecification(workspace, file, 'forks')) as DeprecatedWorkspaceSpec[]
}

describe('base sequencer', () => {
Expand Down

0 comments on commit 6b6099c

Please sign in to comment.