YesNo is an HTTP testing library for Node.js designed to intercept, mock, and record outgoing HTTP requests. Utilizing the `Mitm` library, it operates at a low level within the Node.js process, allowing for the inspection and manipulation of actual HTTP traffic generated by an application, rather than simply mocking higher-level request library calls. This approach enables more accurate and robust testing of an application's real-world network behavior. The current version, 0.0.7, is explicitly noted as a beta release, indicating that its API is subject to change and may undergo breaking modifications before reaching its first stable major release. The project is actively maintained, focusing on reaching this 1.0.0 milestone. Key features include spying on requests, easily mocking responses, and recording interactions for future use or to generate test data.
npm install yesno-httpVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up basic HTTP interception, mock responses, and retrieve specific intercepted requests using `yesno-http` within a test suite. It includes `beforeEach` and `afterEach` hooks for proper setup and teardown.
Consult the latest GitHub README and changelog for any API updates before upgrading. Pin exact versions of `yesno-http` in `package.json` to prevent unexpected breaks.
Use `yesno.restore()` diligently in `afterEach` hooks to ensure cleanup. For long-running connections or specific ports that should be ignored (e.g., database connections), configure `ignorePorts` in `yesno.spy(options: IInterceptOptions)`.
Ensure your test environment is configured to trust the `Mitm` generated certificates if real HTTPS connections are being intercepted and re-encrypted. This often involves setting `NODE_TLS_REJECT_UNAUTHORIZED='0'` in test environments, but use with caution due to security implications, or specifically configuring certificate trust for your testing framework.
Ensure `yesno.restore()` is called after every test or test suite where `yesno.spy()` is used, typically within an `afterEach` or `afterAll` hook in your test runner.
Verify that `yesno.spy()` is called *before* the code under test makes its HTTP requests, and `yesno.restore()` is called *after* all asynchronous operations related to the test are finished, often by ensuring the test case is `async/await` and awaits all network operations. Place `yesno.spy()` in `beforeEach` and `yesno.restore()` in `afterEach` for most unit test setups.