Skip to content

Commit

Permalink
feat: show enqueue status, show test result faster
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed Jun 19, 2022
1 parent 3f8c817 commit ff2ad74
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 15 deletions.
8 changes: 8 additions & 0 deletions samples/basic/test/add.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ describe('addition', () => {
await new Promise(resolve => setTimeout(resolve, 100))
})

it('async task 0.5s', async () => {
await new Promise(resolve => setTimeout(resolve, 500))
})

it('async task 1s', async () => {
await new Promise(resolve => setTimeout(resolve, 1000))
})

it('long task', () => {
let sum = 0
for (let i = 0; i < 2e8; i++)
Expand Down
4 changes: 4 additions & 0 deletions samples/basic/test/snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ describe('snapshots', () => {
it('string', () => {
expect('bc').toMatchSnapshot()
})
it('async', async () => {
await new Promise(resolve => setTimeout(resolve, 200))
expect('bc').toMatchSnapshot()
})
})
48 changes: 36 additions & 12 deletions src/pure/ApiProcess.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { ChildProcess } from 'child_process'
import getPort from 'get-port'
import type { WebSocketEvents } from 'vitest'
import type { File, WebSocketEvents } from 'vitest'
import { getConfig } from '../config'
import { log } from '../log'
import { execWithLog, filterColorFormatOutput, sanitizeFilePath } from './utils'
import { buildWatchClient } from './watch/client'

type Handlers = Partial<WebSocketEvents> & { log?: (msg: string) => void }
type Handlers = Partial<WebSocketEvents> & { log?: (msg: string) => void; onUpdate?: (files: File[]) => void }

export class ApiProcess {
private process?: ChildProcess
Expand All @@ -26,6 +26,26 @@ export class ApiProcess {
this.handlers.log?.(content)
}
}
if (this.handlers.onUpdate) {
const taskUpdateHandler = this.handlers.onTaskUpdate
this.handlers.onTaskUpdate = (packs) => {
taskUpdateHandler && taskUpdateHandler(packs)
if (!this.vitestState)
return

const idMap = this.vitestState.client.state.idMap
const fileSet = new Set<File>()
for (const [id] of packs) {
const task = idMap.get(id)
if (!task)
continue

task.file && fileSet.add(task.file)
}

this.handlers.onUpdate && this.handlers.onUpdate(Array.from(fileSet))
}
}
}

async start() {
Expand Down Expand Up @@ -75,17 +95,21 @@ export class ApiProcess {
this.dispose()
})

this.vitestState = buildWatchClient({
port,
handlers: this.handlers,
})
setTimeout(() => {
this.vitestState = buildWatchClient({
port,
handlers: this.handlers,
reconnectInterval: 100,
reconnectTries: 100,
})

this.vitestState.loadingPromise.then((isRunning) => {
if (!isRunning) {
const files = this.vitestState!.files.value
files && this.handlers?.onFinished?.(files)
}
})
this.vitestState.loadingPromise.then((isRunning) => {
if (!isRunning) {
const files = this.vitestState!.files.value
files && this.handlers?.onFinished?.(files)
}
})
}, 50)
}

get client() {
Expand Down
5 changes: 5 additions & 0 deletions src/pure/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class TestRunner {
? this.defaultVitestCommand
: { cmd: 'npx', args: ['vitest'] },
updateSnapshot = false,
onUpdate?: (files: File[]) => void,
): Promise<{ testResultFiles: File[]; output: string }> {
const command = vitestCommand.cmd
const args = [
Expand Down Expand Up @@ -111,6 +112,10 @@ export class TestRunner {

testResultFiles = files
},
onCollected: (files) => {
files && onUpdate && onUpdate(files)
},
onUpdate,
})

return { testResultFiles, output }
Expand Down
8 changes: 6 additions & 2 deletions src/pure/watch/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ type WebSocketStatus = 'OPEN' | 'CONNECTING' | 'CLOSED';
export type RunState = 'idle' | 'running'

export function buildWatchClient(
{ port, url = `ws://localhost:${port}/__vitest_api__`, handlers }: {
{ port, url = `ws://localhost:${port}/__vitest_api__`, handlers, reconnectInterval, reconnectTries }: {
url?: string
handlers?: Partial<WebSocketEvents>
port: number
reconnectInterval?: number
reconnectTries?: number
},
) {
const client = createClient(url, {
handlers,
WebSocketConstructor: WebSocket as any,
reactive: reactive as any,
reconnectInterval,
reconnectTries,
})

const config = shallowRef<ResolvedConfig>({} as any)
Expand Down Expand Up @@ -54,7 +58,7 @@ export function buildWatchClient(
const loadingPromise = client.waitForConnection().then(async () => {
const files = await client.rpc.getFiles()
const idResultPairs: [string, TaskResult][] = []
let isRunning = false
let isRunning = files.length === 0
files && travel(files)
function travel(tasks: Task[]) {
for (const task of tasks) {
Expand Down
10 changes: 9 additions & 1 deletion src/runHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isAbsolute, relative } from 'path'
import { existsSync } from 'fs'
import * as vscode from 'vscode'
import type { File } from 'vitest'
import { readFile } from 'fs-extra'
import type { FormattedTestResults } from './pure/runner'
import {
Expand Down Expand Up @@ -237,7 +238,7 @@ async function runTest(
}

testCaseSet.forEach((testCase) => {
run.started(testCase)
run.enqueued(testCase)
})

if (mode === 'run' || mode === 'update') {
Expand Down Expand Up @@ -267,6 +268,9 @@ async function runTest(
config.env || undefined,
command,
mode === 'update',
(files: File[]) => {
syncFilesTestStatus(files, discover, ctrl, run, false, false)
},
)

const finishedTests = syncFilesTestStatus(testResultFiles, discover, ctrl, run, true, false)
Expand All @@ -279,6 +283,10 @@ async function runTest(
return
}

testCaseSet.forEach((testCase) => {
run.started(testCase)
})

const pathToFile = new Map<string, vscode.TestItem>()
for (const file of fileItems)
pathToFile.set(sanitizeFilePath(file.uri!.fsPath), file)
Expand Down

0 comments on commit ff2ad74

Please sign in to comment.