Skip to content

Commit

Permalink
chore: render type tests tree
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Oct 2, 2022
1 parent 689361d commit faf4081
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 21 deletions.
7 changes: 5 additions & 2 deletions packages/vitest/src/node/core.ts
Expand Up @@ -152,6 +152,7 @@ export class Vitest {
const testsFilesList = await this.globTestFiles()
const checker = new Typechecker(this, testsFilesList)
checker.onParseEnd(async ({ files, sourceErrors }) => {
await this.report('onCollected', files)
await this.report('onFinished', files)
if (sourceErrors.length && !this.config.typecheck.ignoreSourceErrors) {
process.exitCode = 1
Expand All @@ -166,13 +167,15 @@ export class Vitest {
})
checker.onParseStart(async () => {
await this.report('onInit', this)
this.logger.log(c.cyan('Typechecking...')) // TODO show list of test files?
await this.report('onCollected', checker.getTestFiles())
})
checker.onWatcherRerun(async () => {
const { files } = checker.getResult()
await this.report('onWatcherRerun', files.map(f => f.filepath), 'File change detected. Triggering rerun.')
this.logger.log(c.cyan('Typechecking...')) // TODO show list of test files?
await checker.collectTests()
await this.report('onCollected', checker.getTestFiles())
})
await checker.collectTests()
await checker.start()
}

Expand Down
8 changes: 5 additions & 3 deletions packages/vitest/src/node/reporters/default.ts
@@ -1,5 +1,5 @@
import c from 'picocolors'
import type { UserConsoleLog } from '../../types'
import type { File, UserConsoleLog } from '../../types'
import { BaseReporter } from './base'
import type { ListRendererOptions } from './renderers/listRenderer'
import { createListRenderer } from './renderers/listRenderer'
Expand All @@ -18,11 +18,13 @@ export class DefaultReporter extends BaseReporter {
super.onWatcherStart()
}

onCollected() {
onCollected(files?: File[]) {
if (this.isTTY) {
this.rendererOptions.logger = this.ctx.logger
this.rendererOptions.showHeap = this.ctx.config.logHeapUsage
const files = this.ctx.state.getFiles(this.watchFilters)
this.rendererOptions.mode = this.mode
if (!files)
files = this.ctx.state.getFiles(this.watchFilters)
if (!this.renderer)
this.renderer = createListRenderer(files, this.rendererOptions).start()
else
Expand Down
11 changes: 7 additions & 4 deletions packages/vitest/src/node/reporters/renderers/listRenderer.ts
@@ -1,8 +1,8 @@
import c from 'picocolors'
import cliTruncate from 'cli-truncate'
import stripAnsi from 'strip-ansi'
import type { Benchmark, BenchmarkResult, SuiteHooks, Task } from '../../../types'
import { clearInterval, getTests, notNullish, setInterval } from '../../../utils'
import type { Benchmark, BenchmarkResult, SuiteHooks, Task, VitestRunMode } from '../../../types'
import { clearInterval, getTests, getTypecheckTests, isTypecheckTest, notNullish, setInterval } from '../../../utils'
import { F_RIGHT } from '../../../utils/figures'
import type { Logger } from '../../logger'
import { getCols, getHookStateSymbol, getStateSymbol } from './utils'
Expand All @@ -11,6 +11,7 @@ export interface ListRendererOptions {
renderSucceed?: boolean
logger: Logger
showHeap: boolean
mode: VitestRunMode
}

const DURATION_LONG = 300
Expand Down Expand Up @@ -95,8 +96,10 @@ export function renderTree(tasks: Task[], options: ListRendererOptions, level =
if (task.type === 'test' && task.result?.retryCount && task.result.retryCount > 1)
suffix += c.yellow(` (retry x${task.result.retryCount})`)

if (task.type === 'suite')
suffix += c.dim(` (${getTests(task).length})`)
if (task.type === 'suite' && !isTypecheckTest(task)) {
const tests = options.mode === 'typecheck' ? getTypecheckTests(task) : getTests(task)
suffix += c.dim(` (${tests.length})`)
}

if (task.mode === 'skip' || task.mode === 'todo')
suffix += ` ${c.dim(c.gray('[skipped]'))}`
Expand Down
41 changes: 29 additions & 12 deletions packages/vitest/src/typescript/parser.ts
Expand Up @@ -34,6 +34,8 @@ export class Typechecker {
sourceErrors: [],
}

private _tests: Record<string, FileInformation> | null = {}

private tmpConfigPath?: string

constructor(protected ctx: Vitest, protected files: string[]) {}
Expand All @@ -50,28 +52,37 @@ export class Typechecker {
this._onWatcherRerun = fn
}

protected async collectTests(filepath: string): Promise<FileInformation | null> {
protected async collectFileTests(filepath: string): Promise<FileInformation | null> {
return collectTests(this.ctx, filepath)
}

protected async prepareResults(output: string) {
const typeErrors = await this.parseTscLikeOutput(output)
const testFiles = new Set(this.files)

const sourceDefinitions = (await Promise.all(
this.files.map(filepath => this.collectTests(filepath)),
public async collectTests() {
const tests = (await Promise.all(
this.files.map(filepath => this.collectFileTests(filepath)),
)).reduce((acc, data) => {
if (!data)
return acc
acc[data.filepath] = data
return acc
}, {} as Record<string, FileInformation>)
this._tests = tests
return tests
}

protected async prepareResults(output: string) {
const typeErrors = await this.parseTscLikeOutput(output)
const testFiles = new Set(this.files)

let tests = this._tests

if (!tests)
tests = await this.collectTests()

const sourceErrors: TypeCheckError[] = []
const files: File[] = []

testFiles.forEach((path) => {
const { file, definitions, map, parsed } = sourceDefinitions[path]
const { file, definitions, map, parsed } = tests![path]
const errors = typeErrors.get(path)
files.push(file)
if (!errors)
Expand Down Expand Up @@ -189,10 +200,9 @@ export class Typechecker {
return
if (output.includes('File change detected') && !rerunTriggered) {
this._onWatcherRerun?.()
this._result = {
sourceErrors: [],
files: [],
}
this._result.sourceErrors = []
this._result.files = []
this._tests = null // test structure migh've changed
rerunTriggered = true
}
if (/Found \w+ errors*. Watching for/.test(output)) {
Expand All @@ -214,4 +224,11 @@ export class Typechecker {
public getResult() {
return this._result
}

public getTestFiles() {
return Object.values(this._tests || {}).map(({ file }) => ({
...file,
result: undefined,
}))
}
}
4 changes: 4 additions & 0 deletions packages/vitest/src/utils/tasks.ts
Expand Up @@ -10,6 +10,10 @@ export function getTests(suite: Arrayable<Task>): (Test | Benchmark | TypeCheck)
return toArray(suite).flatMap(s => isAtomTest(s) ? [s] : s.tasks.flatMap(c => isAtomTest(c) ? [c] : getTests(c)))
}

export function isTypecheckTest(suite: Task): suite is Suite {
return TYPECHECK_SUITE in suite
}

export function getTypecheckTests(suite: Arrayable<Task>): Suite[] {
return toArray(suite).flatMap((s) => {
if (s.type !== 'suite')
Expand Down

0 comments on commit faf4081

Please sign in to comment.