@deployagram/nock
v1.0.0
Published
Records the HTTP calls that a Nock scope intercepts, and sends each request and response to the Deployagram Collector, in the same format as the Deployagram HTTP Proxy. Use it instead of the Proxy when tests stub HTTP with Nock.
Readme
@deployagram/nock
Records the HTTP calls that a Nock scope intercepts, and sends each request and response to the Deployagram Collector, in the same format as the Deployagram HTTP Proxy. Use it instead of the Proxy when a test stubs HTTP with Nock.
Usage
import nock from 'nock';
import { recordNock } from '@deployagram/nock';
import { DiagramLoggerClient } from '@deployagram/logger-client';
const logger = new DiagramLoggerClient({ baseUrl: collectorUrl });
const scope = nock('http://localhost:9050').post('/Emailer/email').reply(204);
const recorder = recordNock(scope, { logger, caller: 'DontForget', callee: 'Emailer' });
// ... the test runs ...
await recorder.flush(); // wait until every call has reached the Collector
recorder.stop();Differences from the Proxy
- Nock reports a request before it answers. The response is recorded from the stub's status and reply body, not from what the application reads.
- A stub's body is recorded as it was written. A stub with no body records
Body: null. - A failure to reach the Collector is warned about, and does not fail the call.
- The recorder works on one scope. Record each scope that the test uses.
Testing
npm testThe tests use real Nock and fetch, with a logger that keeps entries in memory. They need no Collector.
