From 5a2c8a69c91324ca9c4bd35f3ba0595c8258512d Mon Sep 17 00:00:00 2001 From: Konstantin Konstantinov Date: Thu, 5 Mar 2026 00:55:02 +0200 Subject: [PATCH] `v2` flaky test fix (#1616) --- test/integration/test/processCleanup.test.ts | 2 +- .../test/server/cloudflareWorkers.test.ts | 24 +++++++++++++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/test/integration/test/processCleanup.test.ts b/test/integration/test/processCleanup.test.ts index f35a287c..780c920f 100644 --- a/test/integration/test/processCleanup.test.ts +++ b/test/integration/test/processCleanup.test.ts @@ -8,7 +8,7 @@ import { Server, StdioServerTransport } from '@modelcontextprotocol/server'; const FIXTURES_DIR = path.resolve(__dirname, './__fixtures__'); describe('Process cleanup', () => { - vi.setConfig({ testTimeout: 5000 }); // 5 second timeout + vi.setConfig({ testTimeout: 15_000 }); // 15 second timeout (needs margin for CI; close() alone can take ~4s for hanging servers) it('server should exit cleanly after closing transport', async () => { const server = new Server( diff --git a/test/integration/test/server/cloudflareWorkers.test.ts b/test/integration/test/server/cloudflareWorkers.test.ts index f469a68d..9c2d73a4 100644 --- a/test/integration/test/server/cloudflareWorkers.test.ts +++ b/test/integration/test/server/cloudflareWorkers.test.ts @@ -150,14 +150,28 @@ export default { it('should handle MCP requests', async () => { expect(env).not.toBeNull(); - const client = new Client({ name: 'test-client', version: '1.0.0' }); - const transport = new StreamableHTTPClientTransport(new URL(`http://127.0.0.1:${PORT}/`)); - - await client.connect(transport); + // Retry connection — wrangler may report "Ready" before it can handle requests + let client!: Client; + let lastError: unknown; + for (let attempt = 0; attempt < 5; attempt++) { + try { + client = new Client({ name: 'test-client', version: '1.0.0' }); + const transport = new StreamableHTTPClientTransport(new URL(`http://127.0.0.1:${PORT}/`)); + await client.connect(transport); + lastError = undefined; + break; + } catch (error) { + lastError = error; + await new Promise(resolve => setTimeout(resolve, 1000)); + } + } + if (lastError) { + throw lastError; + } const result = await client.callTool({ name: 'greet', arguments: { name: 'World' } }); expect(result.content).toEqual([{ type: 'text', text: 'Hello, World!' }]); await client.close(); - }); + }, 30_000); });