# jest-json-extend

> Jest expect matchers for JSON strings with jsonpath supported

Latest version **0.0.1** (published 2022-09-15) · MIT license · 0 weekly downloads

## Install

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

## Health

**Score 25/100 (F)** — status: abandoned.

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

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2022-09-15 |
| First published | 2022-09-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 24.2 KB |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| Author | huaxk |
| Maintainers | huaxkxy |
| Keywords | expect, jest, json, jsonpath, jsonpath-extend, matcher |

## Links

- npm: https://www.npmjs.com/package/jest-json-extend
- npm.io page: https://npm.io/package/jest-json-extend

## Dependencies (3)

- [expect](https://npm.io/package/expect.md) ^27
- [jsonpath-plus](https://npm.io/package/jsonpath-plus.md) ^7.2.0
- [jest-matcher-utils](https://npm.io/package/jest-matcher-utils.md) ^27.5.1

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

- 0.0.1 (latest) — 2022-09-15
- 0.0.0 — 2022-09-14

## README

# Jest json extend

Jest expect matchers for JSON strings with jsonpath supported. Inspirted by [jest-json-matchers](https://github.com/debugmaster/jest-json-matchers), further optimize and add `jsonpath` support.

## New Matchers

### .toBeJSON()

It will pass if input is a string and it can be deserialized by `JSON.parse()`. For example:

```js
expect('{"hello":"world"}').toBeJSON(); // It will pass
expect('<span>Test</span>').toBeJSON(); // It will not pass
```

### .toEqualJSON(jsonObject)

It will pass if input is a valid JSON string and its deserialized value is equal to the value passed to the matcher. It compares based on [`toEqual()`](https://jestjs.io/docs/en/expect#toequalvalue) matcher. For example:

```js
expect('{"hello":"world"}').toEqualJSON({ hello: 'world' }); // It will pass
expect('{\n  "status": 400\n}').toEqualJSON({ status: 200 }); // It will not pass
```

### .toMatcherJSON(jsonObject)

It will pass if input is a valid JSON string and its deserialized value contains the properties of the value passed to the matcher. It matches based on [`toMatchObject()`](https://jestjs.io/docs/en/expect#tomatchobjectobject) matcher. For example:

```js
expect('{"status":202,"body":null}').toMatchJSON({ status: 202 }); // It will pass
expect('{"day":14,"month":3}').toMatchJSON({ month: 12 });
```

### expect.jsonContaining(jsonObject)

It will pass if input is a valid JSON string and its deserialized value contains the properties of the value passed to the matcher, It behaves like [`expect.objectContaining()`](https://jestjs.io/docs/en/expect#expectobjectcontainingobject) matcher. For example:

```js
// It will pass
expect({
  body: '{"message":"This is JSON"}',
}).toEqual({
  body: expect.jsonContaining({ message: 'This is JSON' }),
});
// It will not pass
expect({
  status: 200,
}).toEqual({
  body: expect.jsonContaining({ message: 'Not this one' }),
});
```

## Setup

Add jest-json to your Jest config:

```js
module.exports = {
  // ... other configurations
  setupFilesAfterEnv: ['jest-json-extend'],
};
```

Or load it explicitly

```js
import jest-json-extend;

it('should pass', () => {
    expect('').not.toBeJSON()
})
```

## Example with jsonpath

```js
it('should pass', () => {
  const recevied = JSON.stringify({
    firstName: 'John',
    lastName: 'doe',
    age: 26,
    address: {
      streetAddress: 'naist street',
      city: 'Nara',
      postalCode: '630-0192',
    },
  });
  expect(recevied).toMatchJSON(
    address,
    '$.address[?(@property.match(/city/i))]^'
  );
});
```

## License
[MIT](LICENSE)

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