jest-matcher-http
v1.6.5
Published
Additional Jest matchers for HTTP responses.
Downloads
477
Readme
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 [email protected]: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.