Skip to content

Commit 817a54c

Browse files
fix: ignore passthrough requests for expectRequestsToMatchHandler
1 parent bfee889 commit 817a54c

File tree

3 files changed

+62
-12
lines changed

3 files changed

+62
-12
lines changed

src/expectRequestsToMatchHandlers.test.ts

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
import { expect, it, afterEach } from "vitest";
2-
3-
import { createMockServer, installInterceptor, resetMockServers } from ".";
1+
import { expect, it, afterEach, beforeAll, describe } from "vitest";
2+
3+
import {
4+
createMockServer,
5+
installInterceptor,
6+
passthrough,
7+
resetMockServers,
8+
uninstallInterceptor,
9+
} from ".";
410
import { expectRequestsToMatchHandlers } from "./expectRequestsToMatchHandlers";
511

612
installInterceptor();
@@ -201,3 +207,29 @@ it("logs a persistent handler mismatch for an unhandled request", async () => {
201207
"
202208
`);
203209
});
210+
211+
describe("passthrough", () => {
212+
beforeAll(() => {
213+
uninstallInterceptor();
214+
installInterceptor({ onUnhandled: passthrough });
215+
216+
return () => {
217+
uninstallInterceptor();
218+
installInterceptor();
219+
};
220+
});
221+
222+
it("doesn't throw because of passthrough request", async () => {
223+
const localMockServer = createMockServer("http://0.0.0.0");
224+
225+
// control
226+
localMockServer.get("/test", body);
227+
const response = await fetch("http://0.0.0.0/test");
228+
expect(response.ok).toEqual(true);
229+
230+
await expect(() => fetch("http://0.0.0.0/not-mocked")).rejects.toThrowError();
231+
232+
// real expectation
233+
expect(getThrownMessage()).toBeUndefined();
234+
});
235+
});

src/index.test.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import axios from "axios";
2-
import { expect, it, describe, afterEach, vi, beforeAll } from "vitest";
2+
import { expect, it, describe, afterEach, vi, beforeEach } from "vitest";
33

44
import {
55
createMockServer,
@@ -548,9 +548,8 @@ describe("server-level config", () => {
548548
});
549549

550550
describe("interceptor-level config", () => {
551-
beforeAll(() => {
551+
beforeEach(() => {
552552
uninstallInterceptor();
553-
installInterceptor({ onUnhandled: passthrough });
554553

555554
return () => {
556555
uninstallInterceptor();
@@ -559,6 +558,8 @@ describe("interceptor-level config", () => {
559558
});
560559

561560
it("lets a request passthrough", async () => {
561+
installInterceptor({ onUnhandled: passthrough });
562+
562563
const localMockServer = createMockServer("http://0.0.0.0");
563564

564565
// control
@@ -569,4 +570,14 @@ describe("interceptor-level config", () => {
569570
// real expectation
570571
await expect(() => fetch("http://0.0.0.0/not-mocked")).rejects.toThrowError();
571572
});
573+
574+
it("uses custom onUnhandled", async () => {
575+
installInterceptor({ onUnhandled: () => new Response("custom onUnhandled", { status: 500 }) });
576+
577+
const response = await fetch("https://test.com/test");
578+
579+
expect(response.ok).toEqual(false);
580+
expect(response.status).toEqual(500);
581+
expect(await response.text()).toEqual("custom onUnhandled");
582+
});
572583
});

src/server.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ export const createServer = (setupInterceptor: SetupInterceptor, config?: Interc
77
const HandledRequests = new Map<Handler, Request>();
88
const MatchingLog = new Set<MatchingLogEntry>();
99

10+
const handleUnhandled =
11+
config?.onUnhandled ?? (() => new Response("No matching handler", { status: 404 }));
12+
1013
const dispose = setupInterceptor((request) => {
1114
const requestLog = new Set<MatchingLogEntry>();
1215
const explain = (handler: Handler) => (message: string) => {
@@ -32,14 +35,18 @@ export const createServer = (setupInterceptor: SetupInterceptor, config?: Interc
3235
}
3336
}
3437

35-
for (const logEntry of requestLog) MatchingLog.add(logEntry);
36-
UnhandledRequests.add(request);
38+
const unhandledResponse = handleUnhandled(request);
39+
40+
if (unhandledResponse) {
41+
// This request should have been handled
42+
for (const logEntry of requestLog) MatchingLog.add(logEntry);
43+
UnhandledRequests.add(request);
3744

38-
const onUnhandled =
39-
config?.onUnhandled ??
40-
(() => new Response(JSON.stringify({ message: "No matching handler" }), { status: 404 }));
45+
return unhandledResponse;
46+
}
4147

42-
return onUnhandled(request);
48+
// let the request passthrough
49+
return;
4350
});
4451

4552
return {

0 commit comments

Comments
 (0)