1.0.1 • Published 2 years ago

jest-axios-snapshot v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Jest Axios Snapshot

Test Axios responses using Jest snapshots

build status codecov npm prettier jest

Installation

npm install jest-axios-snapshot

Usage

Add the following to your Jest configuration:

{
  "snapshotSerializers": ["jest-axios-snapshot"]
}

Now use an Axios response to match a Jest snapshot.

import axios from 'axios';

it('should download example.com', async () => {
  const response = await axios.get('https://example.com');
  expect(response).toMatchSnapshot();
});

This was written with axios-test-instance and inline snapshots in mind.

import { request, setTestApp } from 'axios-test-instance';

import { app } from './app';

beforeAll(async () => {
  await setTestApp(app);
});

it('should get data', async () => {
  const response = await request.get('/');

  expect(response).tpMatchInlineSnapshot(`
    HTTP/1.1 200 OK
    Connection: close
    Content-Length: 17
    Content-Type: application/json; charset=utf-8

    {
      "hello": "world"
    }
  `);
});

By default the Date header will be stripped. To set a custom response transformer, use setResponseTransformer

import { setResponseTransformer } from 'jest-axios-snapshot';

setResponseTransformer((response) => {
  // Modify response
  const { etag, ...headers } = response.headers;
  return {
    ...response,
    headers,
  };
});