test: consolidate MCP client/fetch-stub setup into useTestClient helper (#333)

This commit is contained in:
Sampson authored and GitHub committed 2026-08-11 15:13:10 -05:00
1 parent ad288a4ef9
commit 937e85a61f
9 files changed
+211 -351

No files matched your search

+24
View File
@@ -1,5 +1,6 @@
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js';
import { after, before } from 'node:test';
import createMcpServer from '../server.js';
/**
@@ -45,3 +46,26 @@ export function stubFetch(handler: (url: URL) => unknown): () => void {
globalThis.fetch = originalFetch;
};
}
/**
* Registers before/after hooks on the enclosing `describe` block that stub
* the Brave API via `responder` and connect a fresh test client, tearing
* both down afterward. Returns a getter for the connected client.
*/
export function useTestClient(responder: (url: URL) => unknown): () => Client {
let client: Client;
let close: () => Promise<void>;
let restoreFetch: () => void;
before(async () => {
restoreFetch = stubFetch(responder);
({ client, close } = await connectTestClient());
});
after(async () => {
restoreFetch();
await close();
});
return () => client;
}