snapsnap
TypeScript icon, indicating that this package has built-in type declarations

0.2.1 • Public • Published

Snapsnap

Capture, cache, and replay HTTP queries in your tests using Nock.

Coverage Status Codecov Codacy 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.

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 (=default) test runner.

Configuring Jest

Snapsnap is implemented as a test environment that extends Jest's built-in NodeEnvironment.

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

Acknowledgments

Based on Jest Nock Back and Jest Nock.

Package Sidebar

Install

npm i snapsnap

Weekly Downloads

248

Version

0.2.1

License

MIT

Unpacked Size

39.8 kB

Total Files

43

Last publish

Collaborators

  • aleksi.asikainen