Skip to content

Commit

Permalink
fix: fix duration when fake timers are enabled (#1089)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Apr 3, 2022
1 parent 48a82ed commit bec43fc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
7 changes: 4 additions & 3 deletions packages/vitest/src/runtime/collect.ts
@@ -1,4 +1,3 @@
import { performance } from 'perf_hooks'
import { createHash } from 'crypto'
import { relative } from 'pathe'
import type { File, ResolvedConfig, Suite, TaskBase } from '../types'
Expand All @@ -8,6 +7,8 @@ import { processError } from './error'
import { context } from './context'
import { runSetupFiles } from './setup'

const now = Date.now

function hash(str: string, length = 10) {
return createHash('md5')
.update(str)
Expand Down Expand Up @@ -46,9 +47,9 @@ export async function collectTests(paths: string[], config: ResolvedConfig) {
file.tasks.push(c)
}
else {
const start = performance.now()
const start = now()
const suite = await c.collect(file)
file.collectDuration = performance.now() - start
file.collectDuration = now() - start
if (suite.name || suite.tasks.length)
file.tasks.push(suite)
}
Expand Down
15 changes: 8 additions & 7 deletions packages/vitest/src/runtime/run.ts
@@ -1,4 +1,3 @@
import { performance } from 'perf_hooks'
import type { File, HookListener, ResolvedConfig, Suite, SuiteHooks, Task, TaskResult, TaskState, Test } from '../types'
import { vi } from '../integrations/vi'
import { getSnapshotClient } from '../integrations/snapshot/chai'
Expand All @@ -10,6 +9,8 @@ import { rpc } from './rpc'
import { collectTests } from './collect'
import { processError } from './error'

const now = Date.now

function updateSuiteHookState(suite: Task, name: keyof SuiteHooks, state: TaskState) {
if (!suite.result)
suite.result = { state: 'run' }
Expand Down Expand Up @@ -67,11 +68,11 @@ export async function runTest(test: Test) {
return
}

const start = performance.now()
const start = now()

test.result = {
state: 'run',
startTime: Date.now(),
startTime: start,
}
updateTask(test)

Expand Down Expand Up @@ -130,7 +131,7 @@ export async function runTest(test: Test) {

getSnapshotClient().clearTest()

test.result.duration = performance.now() - start
test.result.duration = now() - start

workerState.current = undefined

Expand All @@ -154,11 +155,11 @@ export async function runSuite(suite: Suite) {
return
}

const start = performance.now()
const start = now()

suite.result = {
state: 'run',
startTime: Date.now(),
startTime: start,
}

updateTask(suite)
Expand Down Expand Up @@ -189,7 +190,7 @@ export async function runSuite(suite: Suite) {
suite.result.error = processError(e)
}
}
suite.result.duration = performance.now() - start
suite.result.duration = now() - start

if (suite.mode === 'run') {
if (!hasTests(suite)) {
Expand Down

0 comments on commit bec43fc

Please sign in to comment.