diff --git a/src/api/child_process.ts b/src/api/child_process.ts index 46fc133..40a9853 100644 --- a/src/api/child_process.ts +++ b/src/api/child_process.ts @@ -104,6 +104,7 @@ async function createChildVitestProcess(pkg: VitestPackage) { const runnerOptions: WorkerRunnerOptions = { type: 'init', meta: { + shellType: 'child_process', vitestNodePath: pkg.vitestNodePath, env: getConfig(pkg.folder).env || undefined, configFile: pkg.configFile, diff --git a/src/api/terminal.ts b/src/api/terminal.ts index 4d5f1b6..bd83ce6 100644 --- a/src/api/terminal.ts +++ b/src/api/terminal.ts @@ -38,7 +38,7 @@ export async function createVitestTerminalProcess(pkg: VitestPackage): Promise((resolve, reject) => { wss.once('connection', (ws) => { @@ -74,6 +75,7 @@ export function waitForWsResolvedMeta( const runnerOptions: WorkerRunnerOptions = { type: 'init', meta: { + shellType, vitestNodePath: pkg.vitestNodePath, env: getConfig(pkg.folder).env || undefined, configFile: pkg.configFile, diff --git a/src/debug/api.ts b/src/debug/api.ts index a0c8275..4e3ba9c 100644 --- a/src/debug/api.ts +++ b/src/debug/api.ts @@ -95,7 +95,7 @@ export async function debugTests( let vitest!: ResolvedMeta try { - vitest = await waitForWsResolvedMeta(wss, pkg, true) + vitest = await waitForWsResolvedMeta(wss, pkg, true, 'child_process') const api = new VitestFolderAPI(pkg, vitest) const runner = new TestRunner( controller, diff --git a/src/worker/init.ts b/src/worker/init.ts index 3558d75..f84a533 100644 --- a/src/worker/init.ts +++ b/src/worker/init.ts @@ -28,7 +28,9 @@ export async function initVitest(meta: WorkerMeta, options?: UserConfig) { api: false, // @ts-expect-error private property reporter: undefined, - reporters: [reporter], + reporters: meta.shellType === 'terminal' + ? [reporter, ['default', { isTTY: false }]] + : [reporter], ui: false, includeTaskLocation: true, poolOptions: meta.pnpApi && meta.pnpLoader @@ -79,7 +81,7 @@ export async function initVitest(meta: WorkerMeta, options?: UserConfig) { ], }, ) - reporter.init(vitest) + await vitest.report('onInit', vitest) const configs = vitest.projects.map(p => p.server.config.configFile).filter(c => c != null) return { vitest, diff --git a/src/worker/reporter.ts b/src/worker/reporter.ts index e91ad8b..a418995 100644 --- a/src/worker/reporter.ts +++ b/src/worker/reporter.ts @@ -12,14 +12,14 @@ import { Vitest } from './vitest' export class VSCodeReporter implements Reporter { private rpc!: BirpcReturn - private ctx!: VitestCore + private vitest!: VitestCore private get collecting(): boolean { - return this.ctx.configOverride.testNamePattern?.toString() === `/${Vitest.COLLECT_NAME_PATTERN}/` + return this.vitest.configOverride.testNamePattern?.toString() === `/${Vitest.COLLECT_NAME_PATTERN}/` } - init(ctx: VitestCore) { - this.ctx = ctx + onInit(ctx: VitestCore) { + this.vitest = ctx const server = ctx.server.config.server if (!server.fs.allow.includes(setupFilePath)) server.fs.allow.push(setupFilePath) @@ -75,7 +75,7 @@ export class VSCodeReporter implements Reporter { onTaskUpdate(packs: TaskResultPack[]) { packs.forEach(([taskId, result]) => { - const project = this.ctx.getProjectByTaskId(taskId) + const project = this.vitest.getProjectByTaskId(taskId) // the new version uses browser.parseErrorStacktrace if ('getBrowserSourceMapModuleById' in project) { @@ -89,7 +89,7 @@ export class VSCodeReporter implements Reporter { return } - const task = this.ctx.state.idMap.get(taskId) + const task = this.vitest.state.idMap.get(taskId) const isBrowser = task && task.file?.pool === 'browser' result?.errors?.forEach((error) => { @@ -107,7 +107,7 @@ export class VSCodeReporter implements Reporter { this.rpc.onTaskUpdate(packs) } - async onFinished(files?: RunnerTestFile[], errors: unknown[] = this.ctx.state.getUnhandledErrors()) { + async onFinished(files?: RunnerTestFile[], errors: unknown[] = this.vitest.state.getUnhandledErrors()) { const collecting = this.collecting let output = '' @@ -118,16 +118,16 @@ export class VSCodeReporter implements Reporter { callback() }, }) - const _console = this.ctx.logger.console - const errorStream = this.ctx.logger.errorStream - const outputStream = this.ctx.logger.outputStream - this.ctx.logger.errorStream = writable as any - this.ctx.logger.outputStream = writable as any - this.ctx.logger.console = new Console(writable, writable) - await this.ctx.logger.printUnhandledErrors(errors) - this.ctx.logger.console = _console - this.ctx.logger.errorStream = errorStream - this.ctx.logger.outputStream = outputStream + const _console = this.vitest.logger.console + const errorStream = this.vitest.logger.errorStream + const outputStream = this.vitest.logger.outputStream + this.vitest.logger.errorStream = writable as any + this.vitest.logger.outputStream = writable as any + this.vitest.logger.console = new Console(writable, writable) + await this.vitest.logger.printUnhandledErrors(errors) + this.vitest.logger.console = _console + this.vitest.logger.errorStream = errorStream + this.vitest.logger.outputStream = outputStream } nextTick(() => { this.rpc.onFinished(files || [], output, collecting) diff --git a/src/worker/types.ts b/src/worker/types.ts index ba6ec1f..9fb5b90 100644 --- a/src/worker/types.ts +++ b/src/worker/types.ts @@ -6,6 +6,7 @@ export interface WorkerMeta { configFile?: string workspaceFile?: string env: Record | undefined + shellType: 'terminal' | 'child_process' pnpApi?: string pnpLoader?: string }