# jest-json

> Jest matcher for working with JSON

Latest version **2.0.0** (published 2021-12-09) · MIT license · 0 weekly downloads

## Install

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

## Health

**Score 35/100 (D)** — status: abandoned.

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

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2021-12-09 |
| First published | 2018-07-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 16 |
| Author | Lucas Duailibe |
| Maintainers | duailibe |
| Keywords | json, jest, matcher |

## Links

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

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 2.0.0 (latest) — 2021-12-09
- 1.1.1 — 2021-12-08
- 1.1.0 — 2021-10-07
- 1.0.4 — 2021-03-02
- 1.0.3 — 2021-02-23
- 1.0.2 — 2018-07-20
- 1.0.1 — 2018-07-19
- 1.0.0 — 2018-07-19

## README

# `jest-json`

[![CI](https://img.shields.io/github/workflow/status/duailibe/jest-json/CI.svg)](https://github.com/duailibe/jest-json/actions/workflows/ci.yaml)
[![Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![npm](https://img.shields.io/npm/v/jest-json.svg)](https://npmjs.org/jest-json)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)

Jest matchers to work with JSON strings.

## Setup

**Note:** If you're using Jest < 27.2.5, you should stick to `jest-json@^1.0`.

Add `jest-json` to your Jest config:

```json
{
  "setupTestFrameworkScriptFile": "jest-json"
}
```

Or if you're already using another test framework, create a setup file and require each of them:

```js
require("jest-json");
// require("some-jest-library);
```

## Motivation

Say you need to assert `foo` was called with `foo("url", "{'foo': 'bar', 'spam': 'eggs'}")`:

```js
// option 1
expect(foo).toHaveBeenCalledWith(
  "url",
  JSON.stringify({
    foo: "bar",
    spam: "eggs",
  })
);
```

This test may fail depending on how the second argument was created:

```js
// this will pass the test:
foo(
  "url",
  JSON.stringify({
    foo: "bar",
    spam: "eggs",
  })
);

// this will fail the test:
foo(
  "url",
  JSON.stringify({
    spam: "eggs",
    foo: "bar",
  })
);
```

_See this [repl.it](https://repl.it/@duailibe/jest-json-example) for a working example._

To fix the test you'd have to find in `foo.mock.calls` the call you want, parse the JSON and call `expect().toEqual({ spam: "eggs", foo: "bar" })`.

## Matchers

### `expect.jsonMatching`

In the example above, you can use the `expect.jsonMatching` asymmetric matcher:

```js
expect(foo).toHaveBeenCalledWith(
  "url",
  expect.jsonMatching({
    foo: "bar",
    spam: "eggs",
  })
);
```

You can include other asymmetric matchers inside like:

<!-- prettier-ignore -->
```js
expect.jsonMatching(
  expect.objectContaining({
    foo: expect.stringMatching("bar")
  })
)
```

### `expect().toMatchJSON()`

It's just sugar for calling `JSON.parse()` and then `expect().toEqual()`:

```js
expect(json).toMatchJSON(expected);
// equivalent to:
const tmp = JSON.parse(json);
expect(tmp).toEqual(expected);
```

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