# jest-metadata

> 🦸‍♂️ Superhero power for your Jest reporters! 🦸‍♀️

Latest version **1.6.0** (published 2025-06-10) · MIT license · 0 weekly downloads

## Install

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

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.6.0 |
| Published | 2025-06-10 |
| First published | 2023-03-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=16.14.0 |
| Dependencies | 8 |
| Unpacked size | 489.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Yaroslav Serhieiev |
| Maintainers | yaroslavs |
| Keywords | jest, reporter, metadata, jest-reporter, jest-metadata |

## Links

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

## Dependencies (8)

- [tslib](https://npm.io/package/tslib.md) ^2.5.3
- [bunyamin](https://npm.io/package/bunyamin.md) ^1.5.2
- [node-ipc](https://npm.io/package/node-ipc.md) 9.2.1
- [strip-ansi](https://npm.io/package/strip-ansi.md) ^6.0.0
- [lodash.merge](https://npm.io/package/lodash.merge.md) ^4.6.2
- [funpermaproxy](https://npm.io/package/funpermaproxy.md) ^1.1.0
- [lodash.snakecase](https://npm.io/package/lodash.snakecase.md) ^4.1.1
- [jest-environment-emit](https://npm.io/package/jest-environment-emit.md) ^1.0.8

## Recent versions

- 1.6.0 (latest) — 2025-06-10
- 1.3.0-beta.5 (beta) — 2023-12-01
- 1.5.5 — 2025-05-17
- 1.5.4 — 2024-12-17
- 1.5.3 — 2024-05-17
- 1.5.2 — 2024-04-29
- 1.5.1 — 2024-04-27
- 1.5.0 — 2024-03-15
- 1.4.1 — 2024-02-16
- 1.4.0 — 2024-02-16
- 1.3.1 — 2023-12-08
- 1.3.0 — 2023-12-01
- 1.3.0-beta.4 — 2023-12-01
- 1.3.0-beta.3 — 2023-12-01
- 1.3.0-beta.2 — 2023-11-26
- … 36 more at https://npm.io/package/jest-metadata/versions

## README

[![Stand With Ukraine](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner-direct-single.svg)](https://stand-with-ukraine.pp.ua)

<div align="center">

<img src="https://github.com/wix-incubator/jest-metadata/assets/1962469/09c460b4-054f-42bc-ab2f-26d83dc925d7" width=256 height=256 />

# jest-metadata

🦸‍♂️ Superhero power for your Jest reporters! 🦸‍♀️

[![npm version](https://badge.fury.io/js/jest-metadata.svg?rnd=42)](https://badge.fury.io/js/jest-metadata)

</div>

`jest-metadata` is a library that allows you to attach user-defined data to any `jest-circus` entity like describe blocks, function definitions, test runs, invocations, and more. Custom reporters can access your custom data to produce rich and insightful reports leveraging low-level details from [`jest-circus` events](https://github.com/facebook/jest/blob/8433c5cbcbf139d5174bf254996f9f02297a97c5/packages/jest-types/src/Circus.ts#L43) and any additional data you want to use in your reports.

## 🌟 Features

* Attach custom metadata to Jest entities such as describe blocks, function definitions, test runs, and invocations.
* Access your metadata from your reporters to generate insightful reports.
* Graceful degradation for default test environments (`node`, `jsdom`) and `jest-jasmine2` test runner.
* Custom test environment support via a decorator class.

## 📚 Guidelines

This library is primarily intended for the authors of custom Jest reporters.
Direct usage of `jest-metadata` in test files is not recommended.

To use `jest-metadata`, you should:

* Declare `jest` as a peer dependency (or direct one) in your package.
* Provide your reporter as a class that inherits from `jest-metadata/reporter`.
* Provide your test environment as a decorator class that can inherit from any `WithMetadata(*)` class.
* Think about using a namespace for your metadata, so it doesn't clash with other metadata.

The best live example of how to use `jest-metadata` at the moment is [jest-allure2-reporter].

## 🚀 Quick Start

To get your hands dirty, you can try out `jest-metadata` directly in your project.

Install `jest-metadata` using npm:

```bash
npm install jest-metadata --save-dev
```

In your Jest config, add the following:

```diff
+  "testEnvironment": "jest-metadata/environment-node",
+  "reporters": [
+    "default",
+    "./your-custom-reporter.js",
+  ],
```

If you need to use `jest-metadata` in a JSDOM environment or another custom test environment,
please refer to the [Integrating `jest-metadata` into test environment][jest-environment] guide.

## 📖 Usage

### In a Test File

Attach metadata to test entities using annotations:

```js
import { metadata, $Set } from 'jest-metadata';

// Write your own DSL for attaching metadata to test entities
// Try to namespace your metadata to avoid collisions with other libraries
const $Description = (text) => $Set('mycompany.description', text);

$Description('This is a sample test suite.');
describe('Login flow', () => {

  $Description('This is a login test.');
  it('should login', () => {
    // ...
    metadata.push('mycompany.attachements', [
      { name: 'screenshot', type: 'image/png', filePath: '/path/to/screenshot.png' },
    ]);
    // ...
  });
});
```

### In a Custom Reporter

Access metadata in a custom Jest reporter:

```js
import { state } from 'jest-metadata';
import { JestMetadataReporter } from 'jest-metadata/reporter';

class CustomReporter extends JestMetadataReporter {
  async onTestCaseResult(test, testCaseResult) {
    await super.onTestCaseResult(test, testCaseResult);

    const metadata = state.getTestFileMetadata(test.path).lastTestEntry;

    const titles = [testCaseResult.title, ...testCaseResult.ancestorTitles.slice().reverse()];
    const descriptions = [metadata, ...metadata.ancestors()].map((m) => m.get('mycompany.description', '')).find(x => x);
    const attachments = [metadata, ...metadata.ancestors()].flatMap((m) => m.get('mycompany.attachements', []));

    const n = titles.length;
    for (let i = n - 1; i >= 0; i--) {
      console.log(`${titles[i]}: ${descriptions[i]}`);
    }
    // Output:
    // > Login flow: This is a sample test suite.
    // > should login: This is a login test.
  }
}
```

## 📄 Documentation

For more detailed documentation and examples, see the [`docs` folder].

## 🌐 Contributing

We welcome contributions from the community! To get involved, please follow these steps:

* Check the [GitHub issues] for open tasks or [submit a new issue] if you have a feature request or bug report.
* Fork the repository and create a new branch for your changes.
* Make your changes and submit a pull request.

For more information, see our [Contribution Guidelines].

## 📃 License

This project is licensed under the [MIT License].

[`docs` folder]: ./docs
[GitHub issues]: https://github.com/wix-incubator/jest-metadata/issues
[submit a new issue]: https://github.com/wix-incubator/jest-metadata/issues/new/choose
[Contribution Guidelines]: ./CONTRIBUTING.md
[MIT License]: ./LICENSE
[jest-environment]: ./docs/jest-environment.md
[jest-allure2-reporter]: https://github.com/wix-incubator/jest-allure2-reporter

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