@deployagram/msw
v1.0.0
Published
Records HTTP calls intercepted by MSW (Mock Service Worker) and sends them to the Deployagram Collector, in the same format as the Deployagram HTTP Proxy. Use it instead of the Proxy when tests stub HTTP with MSW.
Readme
@deployagram/msw
Records HTTP calls that MSW intercepts, and sends them to the Deployagram Collector. It is the alternative to the Deployagram HTTP Proxy for tests that stub HTTP with MSW: the calls never leave the test process, so there is no Proxy to route them through.
import { setupServer } from 'msw/node';
import { recordHttp } from '@deployagram/msw';
import { DiagramLoggerClient } from '@deployagram/logger-client';
const server = setupServer(/* your handlers */);
server.listen({ onUnhandledFrame: 'error' });
const recorder = recordHttp(server, {
logger: new DiagramLoggerClient({ baseUrl: 'http://localhost:1152' }),
caller: 'EmailClient',
callees: [{ name: 'Emailer', urlPrefix: 'http://localhost:9001' }],
});
// After each test, before marking the test's end:
await recorder.flush();Each request and response becomes one log entry in the same format the Proxy writes, so the Collector and Deployagram Cloud read them in the same way.
Differences from the Proxy
- The entries are timestamped when the event happens, not by the Collector.
- Repeated header values are joined with commas, not kept as separate values.
- A failure to reach the Collector is reported with a warning, and does not fail the test.
Testing
npm testThe tests use MSW and a logger that keeps entries in memory. They need no Collector.
