diff --git a/.changeset/chatty-owls-care.md b/.changeset/chatty-owls-care.md index 84b6fc76f6..9f39f51bb5 100644 --- a/.changeset/chatty-owls-care.md +++ b/.changeset/chatty-owls-care.md @@ -2,4 +2,4 @@ '@backstage/cli': patch --- -The `backstage-cli repo test` command now sets a default Jest `--workerIdleMemoryLimit` of 1GB. This is the recommended workaround for dealing with Jest workers leaking memory and eventually hitting the heap limit. +The `backstage-cli repo test` command now sets a default Jest `--workerIdleMemoryLimit` of 1GB. If needed to ensure that tests are not run in band, `--maxWorkers=2` is set as well. This is the recommended workaround for dealing with Jest workers leaking memory and eventually hitting the heap limit. diff --git a/packages/cli/src/commands/repo/test.ts b/packages/cli/src/commands/repo/test.ts index 69b3d914ed..22f1a68745 100644 --- a/packages/cli/src/commands/repo/test.ts +++ b/packages/cli/src/commands/repo/test.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +import os from 'os'; import { Command, OptionValues } from 'commander'; import { PackageGraph } from '../../lib/monorepo'; import { paths } from '../../lib/paths'; @@ -91,6 +92,19 @@ export async function command(opts: OptionValues, cmd: Command): Promise { args.push('--workerIdleMemoryLimit=1000M'); } + // In order for the above worker memory limit to work we need to make sure the worker + // count is set to at least 2, as the tests will otherwise run in-band. + // Depending on the mode tests are run with the default count is either cpus-1, or cpus/2. + // This means that if we've got at 4 or more cores we'll always get at least 2 workers, but + // otherwise we need to set the worker count explicitly unless already done. + if ( + os.cpus().length <= 3 && + !includesAnyOf(args, '-i', '--runInBand') && + !args.some(arg => arg.match(/^(--maxWorkers|-w)/)) + ) { + args.push('--maxWorkers=2'); + } + if (opts.since) { removeOptionArg(args, '--since'); }