# 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.

Latest version **30.5.0** (published 2026-08-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install jest-docblock
pnpm add jest-docblock
yarn add jest-docblock
bun add jest-docblock
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; popular repo.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 30.5.0 |
| Published | 2026-08-28 |
| First published | 2017-03-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | ^18.14.0 \|\| ^20.0.0 \|\| ^22.0.0 \|\| >=24.0.0 |
| Dependencies | 1 |
| Unpacked size | 9.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 45461 |
| Maintainers | aaronabramov, simenb, rickhanlonii, openjs-operations, cpojer |

## Links

- npm: https://www.npmjs.com/package/jest-docblock
- Repository: https://github.com/jestjs/jest
- Homepage: https://github.com/jestjs/jest#readme
- Issues: https://github.com/jestjs/jest/issues
- npm.io page: https://npm.io/package/jest-docblock

## Dependencies (1)

- [detect-newline](https://npm.io/package/detect-newline.md) ^3.1.0

## Recent versions

- 30.5.0 (latest) — 2026-08-28
- 30.0.0-rc.1 (next) — 2025-06-09
- 30.4.0 — 2026-05-07
- 30.2.0 — 2025-09-28
- 30.0.1 — 2025-06-18
- 30.0.0 — 2025-06-10
- 30.0.0-beta.8 — 2025-06-04
- 30.0.0-beta.7 — 2025-06-04
- 30.0.0-beta.6 — 2025-06-03
- 30.0.0-beta.3 — 2025-05-27
- 30.0.0-beta.1 — 2025-05-27
- 30.0.0-alpha.7 — 2025-01-30
- 30.0.0-alpha.6 — 2024-08-08
- 30.0.0-alpha.5 — 2024-05-30
- 30.0.0-alpha.4 — 2024-05-12
- … 111 more at https://npm.io/package/jest-docblock/versions

## README

# 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:

```js
/**
 * Stuff goes here!
 */
```

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

```js
/**
 * Pragma incoming!
 *
 * @flow
 */
```

Pragmas can also take arguments:

```js
/**
 * 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

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

## Usage

```js
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.

---
_Source: https://npm.io/package/jest-docblock · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
