Trace a Passing Server Test to Its Actual Endpoint
- Total time
- s
- Result
- Timeout (s)
- 10 s
- You entered
- Requests
- 100
- You entered
This article explains how to trace a passing server test to the actual endpoint it exercises, so you can confirm which URL, method, and handler the test really hits. You’ll learn the practical steps, common pitfalls, and tools that make the trace accurate.
Why a Passing Test Can Point to the Wrong Endpoint
In many test suites, a test that passes may not actually be hitting the endpoint you think it is. This happens because of mock overrides, base URL misconfiguration, or a test double that intercepts the HTTP call before it reaches the real server. For example, a test might call fetch('/api/user') but a global mock rewrites it to fetch('/api/mock-user') or returns a hard‑coded response without any network request. The test passes, but the real endpoint is never touched.
Another common scenario is when the test uses a relative URL and the test runner sets a base URL that points to a staging server, not the local development server you intend. The test passes because the staging server returns a 200, but you are not testing the code you think you are.
How to Trace a Passing Test to Its Actual Endpoint
To trace a passing test, you need to capture the exact HTTP request the test makes and see where it goes. Follow these steps:
- Enable verbose logging in your test runner. For Jest, use
--verbose; for Mocha, use--reporter spec. This shows which test files run but not the HTTP details, so you need more. - Add a network interceptor. Tools like
nock,msw(Mock Service Worker), orsinoncan log every request. For example, withmsw, you can add aconsole.login the request handler to print the URL and method. - Use a proxy or a packet sniffer. Tools like
mitmproxy,Charles Proxy, or eventcpdumpcan capture traffic from your test process. Set your test’s base URL to point to the proxy, then inspect the captured requests. - Inspect the test’s configuration. Look for environment variables like
API_BASE_URLorTEST_BASE_URLthat the test framework reads. Print them in the test setup to see what the test actually uses. - Add a temporary debug endpoint. In your server code, add a
console.login the route handler that prints the request method and path. If the log appears, the test hit that handler.
Common Tools and Techniques for Tracing
Different testing stacks have different tracing methods. Here are the most common:
- Jest + supertest: Supertest can log the request if you pass a callback to
.end(). Alternatively, you can usejest.spyOn(console, 'log')to capture logs from the server. - Mocha + Chai HTTP: Chai HTTP has a
.then()that gives you the response object, but to see the request URL, you can use a custom agent or a proxy. - Cypress (E2E): Cypress has
cy.intercept()which can log all requests. Usecy.intercept('*', (req) => console.log(req.url))to see every request the app makes. - Playwright: Playwright’s
page.on('request')event gives you the URL and method for every request. - Raw Node.js: If you are using plain
fetchoraxios, you can add a request interceptor (axios) or wrapfetchto log the URL.
How to Choose the Right Tracing Method
Choosing a tracing method depends on your test type and what you need to see:
- Unit tests with mocked HTTP: Use a library like
nockormswthat lets you define interceptors. If you want to see the URL, add aconsole.login the interceptor’s handler. - Integration tests that hit a real server: Use a proxy or a packet sniffer, because you need to see the actual network traffic.
- End‑to‑end tests in a browser: Use the browser’s network tab in devtools, or a tool like Cypress’s
cy.intercept. - When you cannot modify the test code: Use an external proxy like
mitmproxyand set your test’s base URL to the proxy’s address.
If you are only interested in the endpoint path, a simple log in the server’s request handler is often the fastest. If you need to see the full request (headers, body), a proxy is better.
Common Problems and Misconceptions
- “The test passes, so it must be hitting the endpoint.” Not necessarily. The test might be using a mock that returns a successful response without any network call.
- “I changed the code, but the test still passes.” This often means the test is not actually exercising the changed code. Trace the request to see if it reaches the updated handler.
- “The URL looks correct, but the test fails.” The URL might be correct but the method is wrong (e.g., POST vs GET). Log the method as well.
- “I use a base URL in my test, so it must be right.” The base URL might be overridden by a mock or a service worker. Always verify the final URL that leaves the test.
- “The test hits a local server, so it’s fine.” Even a local server can be the wrong one if you have multiple instances running on different ports. Check the port.
Comparing Tracing Approaches
Here is a quick comparison of the most common tracing approaches:
- Logging in the server handler: Simple, but only works if you control the server code. It shows the request that actually reached the server.
- Proxy (mitmproxy, Charles): Works for any test, but requires configuration and may slow down the test. It shows the exact bytes sent and received.
- Test framework hooks (Cypress intercept, Playwright request event): Great for E2E tests, but only for browser‑based tests. They show the request as the browser makes it.
- HTTP client interceptors (axios, fetch wrapper): Works for unit and integration tests, but only if you use that client. It shows the request before it goes to the network.
Safety and Best Practices
- Never log sensitive data. When tracing, avoid printing authorization headers or request bodies that contain passwords or personal information.
- Keep tracing temporary. Remove debug logs before committing code to the repository.
- Be mindful of network security. If you use a proxy, make sure it is on a trusted network and not exposed to the public internet.
- Understand the difference between mocking and real requests. A mocked request does not prove that the real server works correctly. Trace both mocked and real scenarios.
Frequently Asked Questions
Why does my test pass but the endpoint is never called?
This often happens because a mock or a service worker intercepts the request before it reaches the network. For example, if you use msw, it will intercept all requests that match its handlers. The test passes because the mock returns a response, but the real endpoint is never contacted. To verify, disable the mock temporarily or add a log in the real server’s handler.
How can I see the exact URL my test is hitting?
You can add a console.log in the test’s HTTP client (e.g., an axios interceptor) or use a proxy. For browser‑based tests, use the browser’s developer tools network tab. For Node.js tests, a simple console.log in the request handler of your server will show the URL if the request reaches it.
What is the difference between a mock and a real request in testing?
A mock is a simulated response that does not involve the network. A real request goes to an actual server. Mocks are fast and deterministic, but they do not validate that the server’s code works. Real requests test the full stack, but they can be slow and flaky. Tracing helps you know which one your test is actually doing.
Can I trace a test that uses a relative URL?
Yes. The relative URL is resolved against a base URL that the test framework or environment provides. To trace it, print the resolved URL in the test setup. For example, if you use fetch('/api/user'), the actual URL might be http://localhost:3000/api/user. You can log the base URL and the final URL.
Is it safe to use a proxy to trace tests?
Yes, but you must ensure the proxy is on a secure network and does not expose sensitive data. Use a local proxy like mitmproxy that runs on your machine. Avoid logging authorization headers or personal data. After tracing, shut down the proxy and remove any logs.
Why does my test hit the wrong endpoint even though the URL looks correct?
This can happen if a global mock or a service worker rewrites the URL. For example, a service worker might intercept /api/user and redirect it to /api/mock. Also, check for environment variables that override the base URL. Use a proxy to see the actual URL that leaves the test.
Related
Formula used
in t | Timeout (s) | s | 10 | 1 in n | Requests | - | 100 | 1 out rate | Requests per second | req/s | n / t | 2 out total | Total time | s | t | 1
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.