From 43f5e8200543d9fea342eec5091c03f0c00dcd9c Mon Sep 17 00:00:00 2001 From: Guillaume Gautreau Date: Tue, 31 Jan 2023 09:31:27 +0100 Subject: [PATCH 1/2] feat: allow to use workerIdleMemoryLimit with only 1 worker or runInBand option --- .../src/__tests__/testSchedulerHelper.test.js | 49 +++++++++++++------ packages/jest-core/src/testSchedulerHelper.ts | 16 ++++-- 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/packages/jest-core/src/__tests__/testSchedulerHelper.test.js b/packages/jest-core/src/__tests__/testSchedulerHelper.test.js index d3cc137013c4..e826551d6974 100644 --- a/packages/jest-core/src/__tests__/testSchedulerHelper.test.js +++ b/packages/jest-core/src/__tests__/testSchedulerHelper.test.js @@ -23,24 +23,43 @@ const getTestMock = () => ({ const getTestsMock = () => [getTestMock(), getTestMock()]; test.each` - tests | timings | detectOpenHandles | maxWorkers | watch | expectedResult - ${[getTestMock()]} | ${[500, 500]} | ${false} | ${undefined} | ${true} | ${true} - ${getTestsMock()} | ${[2000, 500]} | ${false} | ${1} | ${true} | ${true} - ${getTestsMock()} | ${[2000, 500]} | ${false} | ${2} | ${true} | ${false} - ${[getTestMock()]} | ${[2000, 500]} | ${false} | ${undefined} | ${true} | ${false} - ${getTestMock()} | ${[500, 500]} | ${false} | ${undefined} | ${true} | ${false} - ${getTestsMock()} | ${[2000, 500]} | ${false} | ${1} | ${false} | ${true} - ${getTestMock()} | ${[2000, 500]} | ${false} | ${2} | ${false} | ${false} - ${[getTestMock()]} | ${[2000]} | ${false} | ${undefined} | ${false} | ${true} - ${getTestsMock()} | ${[500, 500]} | ${false} | ${undefined} | ${false} | ${true} - ${new Array(45)} | ${[500]} | ${false} | ${undefined} | ${false} | ${false} - ${getTestsMock()} | ${[2000, 500]} | ${false} | ${undefined} | ${false} | ${false} - ${getTestsMock()} | ${[2000, 500]} | ${true} | ${undefined} | ${false} | ${true} + tests | timings | detectOpenHandles | maxWorkers | watch | workerIdleMemoryLimit | expectedResult + ${[getTestMock()]} | ${[500, 500]} | ${false} | ${undefined} | ${true} | ${undefined} | ${true} + ${getTestsMock()} | ${[2000, 500]} | ${false} | ${1} | ${true} | ${undefined} | ${true} + ${getTestsMock()} | ${[2000, 500]} | ${false} | ${2} | ${true} | ${undefined} | ${false} + ${[getTestMock()]} | ${[2000, 500]} | ${false} | ${undefined} | ${true} | ${undefined} | ${false} + ${getTestMock()} | ${[500, 500]} | ${false} | ${undefined} | ${true} | ${undefined} | ${false} + ${getTestsMock()} | ${[2000, 500]} | ${false} | ${1} | ${false} | ${undefined} | ${true} + ${getTestMock()} | ${[2000, 500]} | ${false} | ${2} | ${false} | ${undefined} | ${false} + ${[getTestMock()]} | ${[2000]} | ${false} | ${undefined} | ${false} | ${undefined} | ${true} + ${getTestsMock()} | ${[500, 500]} | ${false} | ${undefined} | ${false} | ${undefined} | ${true} + ${new Array(45)} | ${[500]} | ${false} | ${undefined} | ${false} | ${undefined} | ${false} + ${getTestsMock()} | ${[2000, 500]} | ${false} | ${undefined} | ${false} | ${undefined} | ${false} + ${getTestsMock()} | ${[2000, 500]} | ${true} | ${undefined} | ${false} | ${undefined} | ${true} + ${[getTestMock()]} | ${[500, 500]} | ${false} | ${undefined} | ${true} | ${'500MB'} | ${true} + ${getTestsMock()} | ${[2000, 500]} | ${false} | ${1} | ${true} | ${'500MB'} | ${true} + ${getTestsMock()} | ${[2000, 500]} | ${false} | ${1} | ${false} | ${'500MB'} | ${false} + ${[getTestMock()]} | ${[2000]} | ${false} | ${undefined} | ${false} | ${'500MB'} | ${false} + ${getTestsMock()} | ${[500, 500]} | ${false} | ${undefined} | ${false} | ${'500MB'} | ${false} + ${getTestsMock()} | ${[2000, 500]} | ${true} | ${undefined} | ${false} | ${'500MB'} | ${true} `( 'shouldRunInBand() - should return $expectedResult for runInBand mode', - ({tests, timings, detectOpenHandles, maxWorkers, watch, expectedResult}) => { + ({ + tests, + timings, + detectOpenHandles, + maxWorkers, + watch, + workerIdleMemoryLimit, + expectedResult, + }) => { expect( - shouldRunInBand(tests, timings, {detectOpenHandles, maxWorkers, watch}), + shouldRunInBand(tests, timings, { + detectOpenHandles, + maxWorkers, + watch, + workerIdleMemoryLimit, + }), ).toBe(expectedResult); }, ); diff --git a/packages/jest-core/src/testSchedulerHelper.ts b/packages/jest-core/src/testSchedulerHelper.ts index a913b3958c94..9ecfcf8acf06 100644 --- a/packages/jest-core/src/testSchedulerHelper.ts +++ b/packages/jest-core/src/testSchedulerHelper.ts @@ -13,7 +13,13 @@ const SLOW_TEST_TIME = 1000; export function shouldRunInBand( tests: Array, timings: Array, - {detectOpenHandles, maxWorkers, watch, watchAll}: Config.GlobalConfig, + { + detectOpenHandles, + maxWorkers, + watch, + watchAll, + workerIdleMemoryLimit, + }: Config.GlobalConfig, ): boolean { // detectOpenHandles makes no sense without runInBand, because it cannot detect leaks in workers if (detectOpenHandles) { @@ -41,8 +47,10 @@ export function shouldRunInBand( } return ( - oneWorkerOrLess || - oneTestOrLess || - (tests.length <= 20 && timings.length > 0 && areFastTests) + // When specifying a memory limit, workers should be used + !workerIdleMemoryLimit && + (oneWorkerOrLess || + oneTestOrLess || + (tests.length <= 20 && timings.length > 0 && areFastTests)) ); } From 17b67acffb9d504001bc31cf4f928c2b31f67d11 Mon Sep 17 00:00:00 2001 From: Guillaume Gautreau Date: Tue, 31 Jan 2023 09:53:03 +0100 Subject: [PATCH 2/2] docs: update the documentation --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4997b655331c..1a2d3d9b26df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Features +- `[jest-core]` allow to use workerIdleMemoryLimit with only 1 worker or runInBand option ([#13846](https://github.com/facebook/jest/pull/13846)) + ### Fixes ### Chore & Maintenance