@jest/source-map

中文文档
30.5.2发布于 2026/09/18MITTypeScriptESM + CJS近 30 天下载 2.2亿

Applies source maps to stack traces, so a failing test points at the code you wrote rather than at the code Jest ran.

原始使用文档
文档目录7 节
  1. Install
  2. API
  3. SourceMapSupport#install(sourceMaps?: SourceMapRegistry | n…
  4. SourceMapSupport#getCallsite(level: number, sourceMaps?: So…
  5. getCallsite(level: number, sourceMaps?: SourceMapRegistry |…
  6. SourceMapRegistry
  7. Function names are chosen for readability, not for the spec

@jest/source-map

Applies source maps to stack traces, so a failing test points at the code you wrote rather than at the code Jest ran.

This is a module used internally by Jest. It exists because --enable-source-maps and module.setSourceMapsSupport() do not cover code compiled through vm, which is how Jest evaluates test files, and because Node offers no way to register a map for a filename — Jest serves maps from its own transform pipeline rather than from sourceMappingURL comments on disk.

Install

$ npm install --save @jest/source-map

API

SourceMapSupport#install(sourceMaps?: SourceMapRegistry | null, options?: SourceMapSupportInstallOptions): void

Replaces Error.prepareStackTrace in the current realm, so reading .stack on any error renders frames against the original sources. jest-runner holds one instance per worker and installs once per test file.

sourceMaps maps a transformed file to the .map file Jest wrote into its transform cache — inside Jest, runtime.getSourceMaps(). The sources inside a map resolve with URL semantics against the transformed file, not against the map's own location. Files missing from the registry fall back to a sourceMappingURL comment on the file itself, which covers pre-compiled output that ships its own map.

import {SourceMapSupport} from '@jest/source-map';

const sourceMapSupport = new SourceMapSupport();
sourceMapSupport.install(new Map([['/build/app.js', '/cache/app.js.map']]));

new Error('boom').stack;
// Error: boom
//     at greet (/src/app.ts:12:9)

The formatter stays installed for the lifetime of the process, and each call swaps in a new registry. Nothing is restored, deliberately: an error thrown after a test finishes — a stray timer, a floating promise — is the one users have the hardest time placing, and it would otherwise report a position in the transformed file.

A map that cannot be parsed is reported once via console.warn; pass {suppressWarnings: true} to turn that off.

SourceMapSupport#getCallsite(level: number, sourceMaps?: SourceMapRegistry | null): CallSite

Returns a single remapped CallSite, level frames above the caller. Used for --testLocationInResults. Shares its parsed maps with the installed formatter, so each .map file is read once.

getCallsite(level: number, sourceMaps?: SourceMapRegistry | null): CallSite

Deprecated free-function alias of SourceMapSupport#getCallsite.

SourceMapRegistry

Map<string, string> — transformed file path to source map path.

Function names are chosen for readability, not for the spec

A frame is named after the source map's name at the frame's own position. That name is the identifier being called there rather than the enclosing function, so every frame gets annotated with the call on its line:

at Object.toBeTruthy (assertionCount.test.js:12:17)
at Object.setTimeout (inside.js:9:3)

The spec-correct reading takes the name from the caller's position instead. It is what [email protected] switched to, and it collapses the frames above to Object.<anonymous>, because V8 has no name of its own for a module-level frame. Positions are identical either way — only names differ, and only where V8 could not name the frame.

This package optimises for reading a test failure, so it keeps the annotation. If you need the spec semantics, getCallsite hands back a CallSite you can map yourself.

版本与发布记录

@jest/source-map 共发布 65 个版本,首次发布于 2019/03/05;过去 12 个月发布了 2 个版本。

版本发布日期距今
30.5.2latest2026/09/181 个月前
30.5.02026/08/282 个月前
30.0.12025/06/191.4 年前
30.0.02025/06/101.4 年前
30.0.0-rc.1next2025/06/091.4 年前
30.0.0-beta.82025/06/041.4 年前

关于 @jest/source-map 的常见问题

@jest/source-map 是什么?

Applies source maps to stack traces, so a failing test points at the code you wrote rather than at the code Jest ran.

如何安装 @jest/source-map?

在项目目录运行 npm install @jest/source-map,或使用 pnpm add / yarn add / bun add。

@jest/source-map 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@jest/source-map 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@jest/source-map 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^18.14.0 || ^20.0.0 || ^22.0.0 || >=24.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@jest/source-map 可以商用吗?

@jest/source-map 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@jest/source-map 的下载量有多少?

根据 npm 官方下载统计,@jest/source-map 在 2026/10/07 – 2026/11/05 这 30 天内被下载 216,266,136 次,日均约 7,208,871 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@jest/source-map 还在维护吗?

最新版本 30.5.2 发布于 2026/09/18(1 个月前);至今共发布 65 个版本,其中过去 12 个月发布了 2 个;首次发布于 2019/03/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。