Skip to content

Commit

Permalink
fix: improve execution time counter
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jul 27, 2022
1 parent 53157a3 commit 0d1f1e8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
8 changes: 5 additions & 3 deletions packages/vitest/src/node/reporters/base.ts
Expand Up @@ -209,7 +209,10 @@ export abstract class BaseReporter implements Reporter {
}

const executionTime = this.end - this.start
const threadTime = files.reduce((acc, test) => acc + Math.max(0, test.result?.duration || 0) + Math.max(0, test.collectDuration || 0), 0)
const collectTime = files.reduce((acc, test) => acc + Math.max(0, test.collectDuration || 0), 0)
const setupTime = files.reduce((acc, test) => acc + Math.max(0, test.setupDuration || 0), 0)
const testsTime = files.reduce((acc, test) => acc + Math.max(0, test.result?.duration || 0), 0)
const threadTime = collectTime + testsTime + setupTime

const padTitle = (str: string) => c.dim(`${str.padStart(10)} `)
const time = (time: number) => {
Expand All @@ -232,9 +235,8 @@ export abstract class BaseReporter implements Reporter {
logger.log(padTitle('Tests'), getStateString(tests))
if (this.watchFilters)
logger.log(padTitle('Time'), time(threadTime))

else
logger.log(padTitle('Time'), time(executionTime) + c.gray(` (in thread ${time(threadTime)}, ${(executionTime / threadTime * 100).toFixed(2)}%)`))
logger.log(padTitle('Time'), time(executionTime) + c.gray(` (setup ${time(setupTime)}, collect ${time(collectTime)}, tests ${time(testsTime)})`))

logger.log()
}
Expand Down
6 changes: 4 additions & 2 deletions packages/vitest/src/runtime/collect.ts
Expand Up @@ -47,8 +47,11 @@ export async function collectTests(paths: string[], config: ResolvedConfig) {

clearCollectorContext()
try {
const setupStart = now()
await runSetupFiles(config)

const collectStart = now()
file.setupDuration = collectStart - setupStart
if (config.browser && isBrowser)
await importFromBrowser(filepath)
else
Expand All @@ -66,13 +69,12 @@ export async function collectTests(paths: string[], config: ResolvedConfig) {
file.tasks.push(c)
}
else {
const start = now()
const suite = await c.collect(file)
file.collectDuration = now() - start
if (suite.name || suite.tasks.length)
file.tasks.push(suite)
}
}
file.collectDuration = now() - collectStart
}
catch (e) {
file.result = {
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/types/tasks.ts
Expand Up @@ -38,6 +38,7 @@ export interface Suite extends TaskBase {
export interface File extends Suite {
filepath: string
collectDuration?: number
setupDuration?: number
}

export interface Test<ExtraContext = {}> extends TaskBase {
Expand Down

0 comments on commit 0d1f1e8

Please sign in to comment.