From 4d9b54ba761eb523bd8bd64ad7c7a57221df2f4f Mon Sep 17 00:00:00 2001 From: Andrew Huth Date: Wed, 20 Mar 2024 08:57:27 -0400 Subject: [PATCH] Wait a random amount of time before selecting a port To try to avoid port conflicts when multiple test runs start at the same time. --- CHANGELOG.md | 2 ++ src/Server.ts | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 637da94..34197a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- [fix] Wait a random amount of time before selecting a port, to try to avoid port conflicts when multiple test runs start at the same time [#92](https://github.com/chanzuckerberg/axe-storybook-testing/pull/92) + # 8.0.0 (2024-03-05) - [breaking] Support Node >= 18 [#91](https://github.com/chanzuckerberg/axe-storybook-testing/pull/91) diff --git a/src/Server.ts b/src/Server.ts index adc16a1..a737677 100644 --- a/src/Server.ts +++ b/src/Server.ts @@ -43,6 +43,10 @@ async function getServer(options: Options): Promise { }; } + // Try to prevent port conflicts when multiple test runs start at the exact same time (such as in + // a monorepo). + await waitRandomTime(500); + const localPath = getStaticStorybookPath(options); const port = await portfinder.getPortPromise(); const host = '127.0.0.1'; @@ -78,3 +82,8 @@ function getStaticStorybookPath(options: Options): string { return storybookStaticPath; } + +function waitRandomTime(maxWaitTime: number) { + const waitTime = Math.floor(Math.random() * maxWaitTime); + return new Promise((resolve) => setTimeout(resolve, waitTime)); +}