From bec43fc2f6c3b154893305be8d052b84cd32a8c4 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Sun, 3 Apr 2022 16:12:48 +0300 Subject: [PATCH] fix: fix duration when fake timers are enabled (#1089) --- packages/vitest/src/runtime/collect.ts | 7 ++++--- packages/vitest/src/runtime/run.ts | 15 ++++++++------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/packages/vitest/src/runtime/collect.ts b/packages/vitest/src/runtime/collect.ts index a1217ae7f6cf..3a46458c8d1d 100644 --- a/packages/vitest/src/runtime/collect.ts +++ b/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' @@ -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) @@ -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) } diff --git a/packages/vitest/src/runtime/run.ts b/packages/vitest/src/runtime/run.ts index 29f4dfc03761..1b1bc0e7c7c9 100644 --- a/packages/vitest/src/runtime/run.ts +++ b/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' @@ -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' } @@ -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) @@ -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 @@ -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) @@ -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)) {