jest-matcher-http
TypeScript icon, indicating that this package has built-in type declarations

1.6.0 • Public • Published

jest-matcher-http

Extension for Jest providing http-related matchers.

npm latest version Build states semantic-release: angular

Jest matchers (toReturnHttpCode, toReturnHttpHeader) to expect http responses.

Logs received body and headers if response is not as expected.

Supported http libraries:

Supported content types:

  • application/json
  • text/plain

Installation & Configuration

With npm:

$ npm install --save-dev jest-matcher-http

Add this setting to your jest.config.js:

setupFilesAfterEnv: ['jest-matcher-http'],

There's an example typescript project here: integration_tests/tsProject/. This is used for integration testing this lib to ensure typescript compatibility.

Usage

expect(response).toReturnHttpCode(307);
expect(response).toReturnHttpHeader('Location', '/v1/new-path');

Log Output Example

expected http status code 500 to equal 307

    server responded with body:
    {
      "requestId": "<uuid>",
      "message": "Some helpful information."
    }

    server responded with headers:
    {
      "content-type": "application/json",
      ...
    }

Extended Example

const supertest = require('supertest');
const request = supertest('www.the-host.com');

describe('Example', () => {
    it('should accept empty object', async () => {
      const response = await request
        .post('/v1/objects')
        .send({});

      expect(response).toReturnHttpCode(200);
    })

    it('should redirect', async () => {
      const response = await request
        .get('/v1/old-path');

      expect(response).toReturnHttpCode(307);
      expect(response).toReturnHttpHeader('Location', '/v1/new-path');
    })
});

Contributing

Thank you for investing time to contribute to this project.

Clone the repository:

$ git clone git@github.com:rimesime/jest-matcher-http.git
$ npm ci
$ npm test

If you spot a problem, search if an issue already exists. If a related issue doesn't exist, you can open a new issue providing all relevant information to e.g. reproduce the problem or motivate your change. You are welcome to open a PR for any ticket.

License

This is free software, distributed under the ISC license.

Readme

Keywords

none

Package Sidebar

Install

npm i jest-matcher-http

Weekly Downloads

346

Version

1.6.0

License

ISC

Unpacked Size

38.9 kB

Total Files

20

Last publish

Collaborators

  • rimesime