From b8ba392be6360c8e383cfbb4e7d9f18b66b9a242 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Wed, 18 Dec 2024 10:39:56 -0500 Subject: [PATCH] fix: apply sourcemaps to stack traces from `globalSetup` files Possibly related to #5605 but not sure --- packages/vitest/src/node/project.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/vitest/src/node/project.ts b/packages/vitest/src/node/project.ts index 864a3ed1bed4..74c3c030fd36 100644 --- a/packages/vitest/src/node/project.ts +++ b/packages/vitest/src/node/project.ts @@ -26,6 +26,7 @@ import mm from 'micromatch' import { isAbsolute, join, relative } from 'pathe' import { ViteNodeRunner } from 'vite-node/client' import { ViteNodeServer } from 'vite-node/server' +import { installSourcemapsSupport } from 'vite-node/source-map' import { setup } from '../api/setup' import { isBrowserEnabled, resolveConfig } from './config/resolveConfig' import { serializeConfig } from './config/serializeConfig' @@ -221,6 +222,12 @@ export class TestProject { return } + // Since globalSetup files are executed with vm.runInContext, and Node.js doesn't read VM + // sourcemaps, we need source-map-support to ensure useful stack traces. + installSourcemapsSupport({ + getSourceMap: source => this.runner.moduleCache.getSourceMap(source), + }) + this._globalSetups = await loadGlobalSetupFiles( this.runner, this.config.globalSetup,