-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: TestScheduler now take in count the runInBand config (#7518)
* refactor: review - check for runInBand via worker count in test scheduler watch mode case * test: updates tests for computeRunInBand * refactor: code review * doc: update CHANGELOG * fix; failure on CI due to node8 changes * use named exports instead of asterisk
- Loading branch information
1 parent
85890f2
commit 3c7b110
Showing
5 changed files
with
148 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
packages/jest-cli/src/__tests__/testSchedulerHelper.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
'use strict'; | ||
|
||
import {shouldRunInBand} from '../testSchedulerHelper'; | ||
|
||
const getTestMock = () => ({ | ||
context: { | ||
config: { | ||
runner: 'jest-runner-parallel', | ||
}, | ||
hasteFS: { | ||
matchFiles: jest.fn(() => []), | ||
}, | ||
}, | ||
path: './test/path.js', | ||
}); | ||
|
||
const getTestsMock = () => [getTestMock(), getTestMock()]; | ||
|
||
test.each` | ||
tests | watch | maxWorkers | timings | expectedResult | ||
${[getTestMock()]} | ${true} | ${undefined} | ${[500, 500]} | ${true} | ||
${getTestsMock()} | ${true} | ${1} | ${[2000, 500]} | ${true} | ||
${getTestsMock()} | ${true} | ${2} | ${[2000, 500]} | ${false} | ||
${[getTestMock()]} | ${true} | ${undefined} | ${[2000, 500]} | ${false} | ||
${getTestMock()} | ${true} | ${undefined} | ${[500, 500]} | ${false} | ||
${getTestsMock()} | ${false} | ${1} | ${[2000, 500]} | ${true} | ||
${getTestMock()} | ${false} | ${2} | ${[2000, 500]} | ${false} | ||
${[getTestMock()]} | ${false} | ${undefined} | ${[2000]} | ${true} | ||
${getTestsMock()} | ${false} | ${undefined} | ${[500, 500]} | ${true} | ||
${new Array(45)} | ${false} | ${undefined} | ${[500]} | ${false} | ||
${getTestsMock()} | ${false} | ${undefined} | ${[2000, 500]} | ${false} | ||
`( | ||
'shouldRunInBand() - should return $expectedResult for runInBand mode', | ||
({tests, watch, maxWorkers, timings, expectedResult}) => { | ||
expect(shouldRunInBand(tests, watch, maxWorkers, timings)).toBe( | ||
expectedResult, | ||
); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
'use strict'; | ||
import type {Test} from 'types/TestRunner'; | ||
|
||
const SLOW_TEST_TIME = 1000; | ||
|
||
export function shouldRunInBand( | ||
tests: Array<Test>, | ||
isWatchMode: boolean, | ||
maxWorkers: number, | ||
timings: number[], | ||
) { | ||
// Run in band if we only have one test or one worker available, unless we | ||
// are using the watch mode, in which case the TTY has to be responsive and | ||
// we cannot schedule anything in the main thread. Same logic applies to | ||
// watchAll. | ||
// | ||
// If we are confident from previous runs that the tests will finish | ||
// quickly we also run in band to reduce the overhead of spawning workers. | ||
const areFastTests = timings.every(timing => timing < SLOW_TEST_TIME); | ||
// This apply also when runInBand arg present | ||
// https://github.com/facebook/jest/blob/700e0dadb85f5dc8ff5dac6c7e98956690049734/packages/jest-config/src/getMaxWorkers.js#L14-L17 | ||
const oneWorkerOrLess = maxWorkers <= 1; | ||
const oneTestOrLess = tests.length <= 1; | ||
|
||
if (isWatchMode) { | ||
return oneWorkerOrLess || (oneTestOrLess && areFastTests); | ||
} | ||
|
||
return ( | ||
oneWorkerOrLess || | ||
oneTestOrLess || | ||
(tests.length <= 20 && timings.length > 0 && areFastTests) | ||
); | ||
} |