From fe489432bd048584000c17d3ea4ad1464347d571 Mon Sep 17 00:00:00 2001 From: LuciNyan <22126563+LuciNyan@users.noreply.github.com> Date: Tue, 27 Aug 2024 21:00:35 +0800 Subject: [PATCH] fix(runner): Use `performance.now` instead of `Date.now` for duration (#6382) --- packages/runner/src/collect.ts | 2 +- packages/runner/src/run.ts | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/runner/src/collect.ts b/packages/runner/src/collect.ts index 55ac27428546..57e0a65586ba 100644 --- a/packages/runner/src/collect.ts +++ b/packages/runner/src/collect.ts @@ -17,7 +17,7 @@ import { getHooks, setHooks } from './map' import { collectorContext } from './context' import { runSetupFiles } from './setup' -const now = Date.now +const now = globalThis.performance ? globalThis.performance.now.bind(globalThis.performance) : Date.now export async function collectTests( paths: string[], diff --git a/packages/runner/src/run.ts b/packages/runner/src/run.ts index d27ee69d5bd4..90bf327cf6bc 100644 --- a/packages/runner/src/run.ts +++ b/packages/runner/src/run.ts @@ -27,7 +27,8 @@ import { hasFailed, hasTests } from './utils/tasks' import { PendingError } from './errors' import { callFixtureCleanup } from './fixture' -const now = Date.now +const now = globalThis.performance ? globalThis.performance.now.bind(globalThis.performance) : Date.now +const unixNow = Date.now function updateSuiteHookState( suite: Task, @@ -181,7 +182,7 @@ export async function runTest(test: Test | Custom, runner: VitestRunner): Promis test.result = { state: 'run', - startTime: start, + startTime: unixNow(), retryCount: 0, } updateTask(test, runner) @@ -376,7 +377,7 @@ export async function runSuite(suite: Suite, runner: VitestRunner): Promise