test: consolidate MCP client/fetch-stub setup into useTestClient helper (#333)
This commit is contained in:
1 parent
ad288a4ef9
commit
937e85a61f
9 files changed
+211
-351
No files matched your search
@@ -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;
|
||||
}
|
||||
Reference in new issue
Block a user