From b435f03c8d0cdd1e00810d41e55c272db8fba036 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Fri, 8 May 2026 10:07:03 +0000 Subject: [PATCH] fix: register connect listener before initiating requests in close-and-destroy test The 'close should still reconnect' test registered the 'connect' listener after makeRequest() was called. On fast connections (like localhost on Windows), the connection could complete before the listener was registered, causing the socket to never be destroyed and making the test flaky. Move the client.once('connect', ...) registration before makeRequest() to ensure the listener is always in place when the connection is established. --- test/close-and-destroy.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/close-and-destroy.js b/test/close-and-destroy.js index 582bd8cbaba..09998892e57 100644 --- a/test/close-and-destroy.js +++ b/test/close-and-destroy.js @@ -166,6 +166,10 @@ test('close should still reconnect', async (t) => { const client = new Client(`http://localhost:${server.address().port}`) after(() => client.destroy()) + client.once('connect', () => { + client[kSocket].destroy() + }) + t.ok(makeRequest()) t.ok(!makeRequest()) @@ -173,9 +177,6 @@ test('close should still reconnect', async (t) => { t.ifError(err) t.strictEqual(client.closed, true) }) - client.once('connect', () => { - client[kSocket].destroy() - }) function makeRequest () { client.request({ path: '/', method: 'GET' }, (err, data) => {