Skip to content

Commit

Permalink
feat: use --api on debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed Jun 19, 2022
1 parent 36cc319 commit 77fd7b3
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 220 deletions.
80 changes: 60 additions & 20 deletions src/pure/ApiProcess.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ChildProcess } from 'child_process'
import type { ChildProcess, SpawnOptionsWithStdioTuple, StdioNull, StdioPipe } from 'child_process'
import getPort from 'get-port'
import type { File, WebSocketEvents } from 'vitest'
import { getConfig } from '../config'
Expand All @@ -7,6 +7,14 @@ import { execWithLog, filterColorFormatOutput, sanitizeFilePath } from './utils'
import { buildWatchClient } from './watch/client'

type Handlers = Partial<WebSocketEvents> & { log?: (msg: string) => void; onUpdate?: (files: File[]) => void }
export interface StartConfig {
cmd: string
args: string[]
cfg: Partial<SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioPipe>>
log: (line: string) => void
onProcessEnd: () => void
registerOnTestFinished: (onTestFinished: () => void) => void
}

export class ApiProcess {
private process?: ChildProcess
Expand All @@ -20,6 +28,7 @@ export class ApiProcess {
private workspace: string,
private handlers: Handlers = {},
private recordOutput = false,
private customStartProcess?: (config: StartConfig) => void,
) {
if (this.handlers.log && !this.handlers.onUserConsoleLog) {
this.handlers.onUserConsoleLog = ({ content }) => {
Expand Down Expand Up @@ -55,8 +64,6 @@ export class ApiProcess {
this.started = true
const port = await getPort()

const logs = [] as string[]
let timer: any
const _log = (line: string) => {
line = filterColorFormatOutput(line)
log.info(line)
Expand All @@ -67,6 +74,9 @@ export class ApiProcess {
log.info('[RUN]', `${this.vitest.cmd} ${this.vitest.args.join(' ')}`)
const cwd = sanitizeFilePath(this.workspace)
log.info('[RUN.cwd]', cwd)

const logs = [] as string[]
let timer: any
const debouncedLog = (line: string) => {
logs.push(line)
if (this.recordOutput)
Expand All @@ -78,22 +88,32 @@ export class ApiProcess {
logs.length = 0
}, 200)
}
this.process = execWithLog(
this.vitest.cmd,
[...this.vitest.args, '--api', port.toString()],
{
cwd,
env: { ...process.env, ...getConfig(this.workspace).env },
},
debouncedLog,
debouncedLog,
).child

this.process.on('exit', () => {
log.info('API PROCESS EXIT')
this.handlers.onFinished?.()
this.dispose()
})
if (!this.customStartProcess) {
this._start(debouncedLog, port, cwd)
}
else {
this.customStartProcess({
cmd: this.vitest.cmd,
args: [...this.vitest.args, '--api', port.toString()],
cfg: {
cwd,
env: { ...process.env, ...getConfig(this.workspace).env },
},
log: debouncedLog,
onProcessEnd: () => {
log.info('API PROCESS EXIT')
this.handlers.onFinished?.()
this.dispose()
},
registerOnTestFinished: (onTestFinished: () => void) => {
const onFinished = this.handlers.onFinished
this.handlers.onFinished = (...args) => {
onFinished?.(...args)
onTestFinished()
}
},
})
}

setTimeout(() => {
this.vitestState = buildWatchClient({
Expand All @@ -112,6 +132,25 @@ export class ApiProcess {
}, 50)
}

private _start(debouncedLog: (line: string) => void, port: number, cwd: string) {
this.process = execWithLog(
this.vitest.cmd,
[...this.vitest.args, '--api', port.toString()],
{
cwd,
env: { ...process.env, ...getConfig(this.workspace).env },
},
debouncedLog,
debouncedLog,
).child

this.process.on('exit', () => {
log.info('API PROCESS EXIT')
this.handlers.onFinished?.()
this.dispose()
})
}

get client() {
return this.vitestState?.client
}
Expand All @@ -129,6 +168,7 @@ export function runVitestWithApi(
vitest: { cmd: string; args: string[] },
workspace: string,
handlers: Handlers,
customStartProcess?: (config: StartConfig) => void,
): Promise<string> {
log.info('[Execute Vitest]', vitest.cmd, vitest.args.join(' '))
return new Promise<string>((resolve) => {
Expand All @@ -144,7 +184,7 @@ export function runVitestWithApi(
resolve(process.output.join(''))
}
},
}, true)
}, true, customStartProcess)
process.start()
})
}
4 changes: 3 additions & 1 deletion src/pure/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
sanitizeFilePath,
} from './utils'
import { isWindows } from './platform'
import type { StartConfig } from './ApiProcess'
import { runVitestWithApi } from './ApiProcess'

export function getDebuggerConfig() {}
Expand Down Expand Up @@ -79,6 +80,7 @@ export class TestRunner {
: { cmd: 'npx', args: ['vitest'] },
updateSnapshot = false,
onUpdate?: (files: File[]) => void,
customStartProcess?: (config: StartConfig) => void,
): Promise<{ testResultFiles: File[]; output: string }> {
const command = vitestCommand.cmd
const args = [
Expand Down Expand Up @@ -116,7 +118,7 @@ export class TestRunner {
files && onUpdate && onUpdate(files)
},
onUpdate,
})
}, customStartProcess)

return { testResultFiles, output }

Expand Down

0 comments on commit 77fd7b3

Please sign in to comment.