# snapsnap

> Tools for snapshotting, replaying, and mocking HTTP queries with Jest

Latest version **0.2.1** (published 2022-06-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install snapsnap
pnpm add snapsnap
yarn add snapsnap
bun add snapsnap
```

## 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.2.1 |
| Published | 2022-06-06 |
| First published | 2020-01-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 8.0.0 |
| Dependencies | 2 |
| Unpacked size | 38.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Aleksi Asikainen |
| Maintainers | aleksi.asikainen |
| Keywords | jest, http, api, snapshot, mock, integration, test, assertion, expect, request, nock |

## Links

- npm: https://www.npmjs.com/package/snapsnap
- Repository: https://github.com/salieri/snapsnap
- Homepage: https://github.com/salieri/snapsnap#readme
- Issues: https://github.com/salieri/snapsnap/issues
- npm.io page: https://npm.io/package/snapsnap

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [async-lock](https://npm.io/package/async-lock.md) ^1.3.1

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 0.2.1 (latest) — 2022-06-06
- 0.2.0 — 2022-06-06
- 0.1.3 — 2020-01-04
- 0.1.2 — 2020-01-04
- 0.1.1 — 2020-01-04
- 0.1.0 — 2020-01-04

## README

# Snapsnap

Capture, cache, and replay HTTP queries in your tests using [Nock](https://github.com/nock/nock). 

[![Coverage Status](https://coveralls.io/repos/github/salieri/snapsnap/badge.svg?branch=master)](https://coveralls.io/github/salieri/snapsnap?branch=master)
[![Codecov](https://codecov.io/gh/salieri/snapsnap/branch/master/graph/badge.svg)](https://codecov.io/gh/salieri/snapsnap)
[![Codacy](https://api.codacy.com/project/badge/Grade/8dd26d6a15764bb3a1c0b6c244c28218)](https://www.codacy.com/app/salieri/snapsnap?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=salieri/snapsnap&amp;utm_campaign=Badge_Grade)
[![Maintainability](https://api.codeclimate.com/v1/badges/03f89b0f947a52e6c5d2/maintainability)](https://codeclimate.com/github/salieri/snapsnap/maintainability)

1. On the first run, the tests make real HTTP queries.
2. The queries and the responses are stored in `__http__/`.
3. On subsequent runs, cached responses are loaded from `__http__/` and used instead of real queries.
4. This is super helpful if your tests run in an environment that has no internet access, or you wish to maintain a snapshot of the responses.

## Usage

Snapsnap behaves similarly to Jest Snapshots. If an HTTP query has not been snapshotted, a real HTTP query will be made. Otherwise, a cached version of the query will be returned.

To signal that a test should be snapshotted, simply use `it.snap()` instead of `it()`. The framework will manage the rest.

```js
it.snap(
  'should query the API',
  async () => {
    const result = await request('https://postman-echo.com/get?foo1=bar1&foo2=bar2');
    const json = await result.json();
    
    expect(json).toMatchSnapshot();
  }
);
```

## Setting Up

### Jest
Supports Jest with [Circus](https://www.npmjs.com/package/jest-circus) (=default) test runner. 

#### Configuring Jest
Snapsnap is implemented as a [test environment](https://jestjs.io/docs/configuration#testenvironment-string) that extends Jest's built-in `NodeEnvironment`.

```js
// ./jest.config.js
module.exports = {
  testEnvironment: 'snapsnap/jest-env.js'
};
```

## Acknowledgments

Based on [Jest Nock Back](https://github.com/jonjaques/jest-nock-back) and [Jest Nock](https://github.com/spring-media/jest-nock).

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