Skip to content

Commit

Permalink
fix: print report to the terminal if shellType is set to terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Dec 12, 2024
1 parent 7b96bde commit 61b3e7a
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/api/child_process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/api/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function createVitestTerminalProcess(pkg: VitestPackage): Promise<R
log.info('[API]', `Initiated ws connection via ${wsAddress}`)
log.info('[API]', `Starting ${formatPkg(pkg)} in the terminal: ${command}`)
terminal.sendText(command, true)
const meta = await waitForWsResolvedMeta(wss, pkg, false)
const meta = await waitForWsResolvedMeta(wss, pkg, false, 'terminal')
const processId = (await terminal.processId) ?? meta.process.id
log.info('[API]', `${formatPkg(pkg)} terminal process ${processId} created`)
const vitestProcess = new VitestTerminalProcess(
Expand Down
2 changes: 2 additions & 0 deletions src/api/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function waitForWsResolvedMeta(
wss: WebSocketServer,
pkg: VitestPackage,
debug: boolean,
shellType: 'terminal' | 'child_process',
) {
return new Promise<ResolvedMeta>((resolve, reject) => {
wss.once('connection', (ws) => {
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/debug/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions src/worker/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
34 changes: 17 additions & 17 deletions src/worker/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import { Vitest } from './vitest'

export class VSCodeReporter implements Reporter {
private rpc!: BirpcReturn<VitestEvents, VitestMethods>
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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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) => {
Expand All @@ -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 = ''
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/worker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface WorkerMeta {
configFile?: string
workspaceFile?: string
env: Record<string, any> | undefined
shellType: 'terminal' | 'child_process'
pnpApi?: string
pnpLoader?: string
}
Expand Down

0 comments on commit 61b3e7a

Please sign in to comment.