Mock Axios vs Network Layer Interception: Which Guarantees No Production Calls?
0 reputation · 03 Dec 2021, 22:32 UTC
0 reputation · 03 Dec 2021, 22:32 UTC
The goal of a test suite for an Axios‑based integration is to exercise all request logic—interceptors, transforms, error shaping—while ensuring that no real outbound traffic reaches a production endpoint. The key constraint is a hard guarantee that accidental real calls cannot slip through, even when the code under test uses axios.create or custom adapters.
Two documented strategies exist:
jest.mock('axios') or axios-mock-adapter. This replaces the Axios module or its adapter, so interceptors and validateStatus are bypassed unless explicitly re‑instantiated.nock or msw/node. These hook into Node’s http layer (or the browser XHR layer), exercising the full Axios HTTP adapter while still allowing request stubbing.Both approaches can prevent real traffic, but their interaction with Axios internals is subtle. The unresolved decision is whether to choose one interception layer per suite or to combine them for higher fidelity, and how to handle axios.create instances when using module mocks.
axios-mock-adapter guarantee that interceptors and validateStatus logic still run, or must we manually mock axios.create to preserve that behavior?nock.disableNetConnect() is active, can a request still reach the network if it is routed through an axios-mock-adapter instance?34775 reputation · 04 Dec 2021, 07:24 UTC
The choice depends on whether you are testing the configuration of Axios (interceptors, transforms) or the behavior of your application. To ensure no accidental real calls slip through, a multi-layered approach is often required.
Yes, axios-mock-adapter works by replacing the Axios adapter rather than the entire module. Because it uses the standard Axios request lifecycle, all interceptors, transforms, and validateStatus logic execute normally. However, if you use axios.create(), you must ensure the adapter is attached to the specific instance or mock the axios.create method to return an instance with the mocked adapter.
No. If axios-mock-adapter handles a request, the traffic is intercepted within the Axios layer and never reaches the Node.js http or https modules. nock.disableNetConnect() only triggers an error if the request bypasses the mock adapter (e.g., via a native fetch call or an unmocked Axios instance) and attempts to hit the actual network stack.
The most robust strategy is a combination of application-level mocking and a network-level safety net:
msw or axios-mock-adapter to define expected behaviors. This ensures your interceptors and logic are actually exercised.nock.disableNetConnect() or MSW's onUnhandled: 'error'. This forces any request not explicitly defined in your mocks to throw an immediate error rather than potentially hitting a production endpoint.axios.create(), ensure your mocking strategy targets the instance specifically or use a network-level interceptor that catches traffic regardless of how the instance was created.Assumption: This analysis assumes a Node.js testing environment. For browser-based testing (Playwright/Cypress), nock is not applicable, and MSW is the standard for intercepting at the browser XHR/Fetch level.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.