jest-docblock

中文文档
30.5.0发布于 2026/08/28MITTypeScriptESM + CJS近 30 天下载 2.2亿

jest-docblock is a package that can extract and parse a specially-formatted comment called a "docblock" at the top of a file.

原始使用文档
文档目录8 节
  1. Installation
  2. Usage
  3. API Documentation
  4. extract(contents: string): string
  5. strip(contents: string): string
  6. parse(docblock: string): {[key: string]: string | string[] }
  7. parseWithComments(docblock: string): { comments: string, pr…
  8. print({ comments?: string, pragmas?: {[key: string]: string…

jest-docblock

jest-docblock is a package that can extract and parse a specially-formatted comment called a "docblock" at the top of a file.

A docblock looks like this:

/**
 * Stuff goes here!
 */

Docblocks can contain pragmas, which are words prefixed by @:

/**
 * Pragma incoming!
 *
 * @flow
 */

Pragmas can also take arguments:

/**
 * Check this out:
 *
 * @myPragma it is so cool
 */

jest-docblock can:

  • extract the docblock from some code as a string
  • parse a docblock string's pragmas into an object
  • print an object and some comments back to a string

Installation

# with yarn
$ yarn add jest-docblock
# with npm
$ npm install jest-docblock

Usage

const code = `
/**
 * Everything is awesome!
 *
 * @everything is:awesome
 * @flow
 */

 export const everything = Object.create(null);
 export default function isAwesome(something) {
   return something === everything;
 }
`;

const {
  extract,
  strip,
  parse,
  parseWithComments,
  print,
} = require('jest-docblock');

const docblock = extract(code);
console.log(docblock); // "/**\n * Everything is awesome!\n * \n * @everything is:awesome\n * @flow\n */"

const stripped = strip(code);
console.log(stripped); // "export const everything = Object.create(null);\n export default function isAwesome(something) {\n return something === everything;\n }"

const pragmas = parse(docblock);
console.log(pragmas); // { everything: "is:awesome", flow: "" }

const parsed = parseWithComments(docblock);
console.log(parsed); // { comments: "Everything is awesome!", pragmas: { everything: "is:awesome", flow: "" } }

console.log(print({pragmas, comments: 'hi!'})); // /**\n * hi!\n *\n * @everything is:awesome\n * @flow\n */;

API Documentation

extract(contents: string): string

Extracts a docblock from some file contents. Returns the docblock contained in contents. If contents did not contain a docblock, it will return the empty string ("").

strip(contents: string): string

Strips the top docblock from a file and return the result. If a file does not have a docblock at the top, then return the file unchanged.

parse(docblock: string): {[key: string]: string | string[] }

Parses the pragmas in a docblock string into an object whose keys are the pragma tags and whose values are the arguments to those pragmas.

parseWithComments(docblock: string): { comments: string, pragmas: {[key: string]: string | string[]} }

Similar to parse except this method also returns the comments from the docblock. Useful when used with print().

print({ comments?: string, pragmas?: {[key: string]: string | string[]} }): string

Prints an object of key-value pairs back into a docblock. If comments are provided, they will be positioned on the top of the docblock.

版本与发布记录

jest-docblock 共发布 126 个版本,首次发布于 2017/03/03;过去 12 个月发布了 2 个版本。

版本发布日期距今
30.5.0latest2026/08/282 个月前
30.4.02026/05/086 个月前
30.2.02025/09/281.1 年前
30.0.12025/06/191.4 年前
30.0.02025/06/101.4 年前
30.0.0-rc.1next2025/06/091.4 年前

关于 jest-docblock 的常见问题

jest-docblock 是什么?

jest-docblock is a package that can extract and parse a specially-formatted comment called a "docblock" at the top of a file.

如何安装 jest-docblock?

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

jest-docblock 支持 TypeScript 吗?

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

jest-docblock 支持 ESM 还是 CommonJS?

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

jest-docblock 需要什么版本的 Node.js?

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

jest-docblock 可以商用吗?

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

jest-docblock 的下载量有多少?

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

jest-docblock 还在维护吗?

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