From 98b29cbcb971da693d4cbb03cf173899c7fdd6f6 Mon Sep 17 00:00:00 2001 From: Hays Clark Date: Tue, 15 Sep 2020 17:59:58 -0700 Subject: [PATCH] fix: port collisions in createTestContext (#1326) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - during testing, get-port often re-uses the same port, due to the delay between when a free-port is assigned to a test, and when the port is actually used. Simply supplying an initial random port value to get-port dramatically reduces the chance of a port being used multiple times in a test run. - testing on CI environments should still utilize Jest’s `—runInBand` flag --- src/testing/testing.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/testing/testing.ts b/src/testing/testing.ts index e7a143ac7..d41683fb4 100644 --- a/src/testing/testing.ts +++ b/src/testing/testing.ts @@ -44,7 +44,8 @@ export async function createTestContext(opts?: CreateTestContextOptions): Promis await Layout.create({ entrypointPath: opts?.entrypointPath, projectRoot: opts?.projectRoot }) ) const pluginManifests = await PluginWorktime.getUsedPlugins(layout) - const randomPort = await getPort({ port: getPort.makeRange(4000, 6000) }) + const randomStartPort = Math.floor(Math.random() * (6000 - 4000)) + 4000 + const randomPort = await getPort({ port: getPort.makeRange(randomStartPort, 6000) }) const privateApp = app as PrivateApp const forcedServerSettings = {