chore: close all clients in fixture (#878)

This commit is contained in:
Pavel Feldman
2025-08-14 10:57:07 -07:00
committed by GitHub
parent badfd82202
commit c92aefdc12

View File

@@ -69,7 +69,7 @@ export const test = baseTest.extend<TestFixtures & TestOptions, WorkerFixtures>(
startClient: async ({ mcpHeadless, mcpBrowser, mcpMode }, use, testInfo) => {
const configDir = path.dirname(test.info().config.configFile!);
let client: Client | undefined;
const clients: Client[] = [];
await use(async options => {
const args: string[] = [];
@@ -87,7 +87,7 @@ export const test = baseTest.extend<TestFixtures & TestOptions, WorkerFixtures>(
args.push(`--config=${path.relative(configDir, configFile)}`);
}
client = new Client({ name: options?.clientName ?? 'test', version: '1.0.0' }, options?.roots ? { capabilities: { roots: {} } } : undefined);
const client = new Client({ name: options?.clientName ?? 'test', version: '1.0.0' }, options?.roots ? { capabilities: { roots: {} } } : undefined);
if (options?.roots) {
client.setRequestHandler(ListRootsRequestSchema, async request => {
if (options.rootsResponseDelay)
@@ -104,12 +104,13 @@ export const test = baseTest.extend<TestFixtures & TestOptions, WorkerFixtures>(
process.stderr.write(data);
stderrBuffer += data.toString();
});
clients.push(client);
await client.connect(transport);
await client.ping();
return { client, stderr: () => stderrBuffer };
});
await client?.close();
await Promise.all(clients.map(client => client.close()));
},
wsEndpoint: async ({ }, use) => {
@@ -126,6 +127,8 @@ export const test = baseTest.extend<TestFixtures & TestOptions, WorkerFixtures>(
await use({
endpoint: `http://localhost:${port}`,
start: async () => {
if (browserContext)
throw new Error('CDP server already exists');
browserContext = await chromium.launchPersistentContext(testInfo.outputPath('cdp-user-data-dir'), {
channel: mcpBrowser,
headless: true,