Licence
MIT
Version
0.1.0
Deps
3
Size
54 kB
Vulns
0
Weekly
0
@n8n-probe/mock-http
HTTP mocking for n8n nodes that call external APIs, built on
MSW for the fast in-process tier and
testcontainers + WireMock for the opt-in
real-server tier.
import {
setupMswForTest,
mockApi,
presets,
createMockHttpExecuteFunctions,
} from '@n8n-probe/mock-http';
import { MyHttpNode } from '../nodes/MyHttpNode/MyHttpNode.node';
const server = setupMswForTest();
it('reads a user from the API', async () => {
server.use(...mockApi().get('https://api.example.com/users/1').reply(200, { id: 1 }).handlers());
const ctx = createMockHttpExecuteFunctions({
input: [{ json: {} }],
params: { url: 'https://api.example.com/users/1' },
});
const out = await new MyHttpNode().execute.call(ctx);
expect(out[0][0].json).toEqual({ id: 1 });
});
MSW tier (part of pnpm test)
setupMswForTest(handlers?)— one shared server per test file, wired tobeforeAll/afterEach/afterAll. An unmatched request fails the test (onUnhandledRequest: 'error') instead of hanging or hitting the network. Returns the server for per-caseserver.use(...).mockApi()— fluent handler builder:mockApi().get(url).reply(200, body).post(url2).reply(201).handlers().body:string→ text,ArrayBuffer/ typed array → binary, anything else → JSON, omitted → empty.presets— ready-made handlers for the failure modes node authors get wrong:rateLimited(path)(429 +Retry-After: 1),timeout(path)(never settles — set a bounded requesttimeout),flakyThenSuccess(path, n, body?)(503 for the firstncalls, then 200).createMockHttpExecuteFunctions(options?)—@n8n-probe/core's mock context withhelpers.httpRequestwired to a real axios client, so a node's outbound calls are actually intercepted. Same options ascreateMockExecuteFunctions.performHttpRequest(node, options)— the axios-backedhelpers.httpRequeststand-in on its own. Maps the commonIHttpRequestOptionsfields; a non-2xx response (unlessignoreHttpStatusErrors) or a transport failure is thrown asNodeApiError.
WireMock tier (opt-in, needs Docker — pnpm test:e2e:full)
startWireMock({ mappingsDir?, image? })— bootswiremock/wiremockviatestcontainersand returns{ baseUrl, stop() }. Use it for reusable stub mappings, latency and fault injection, or cross-language contract stubs.testcontainersis an optional peer dependency. Alwaysawait stop().
MSW vs WireMock: MSW is in-process, millisecond-fast and needs no Docker — use it for the default suite. WireMock is a real HTTP server in a container — use it when the stubs must be shared across teams or languages, or when you need realistic network behaviour (latency, connection resets, proxying, record & replay).
Part of n8n-probe. Not affiliated with n8n GmbH.