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,