From 0e8934e4a1466b13a801cf3f9de097cd9ecf4566 Mon Sep 17 00:00:00 2001 From: Daniel Schmidt Date: Sat, 3 Mar 2018 12:41:25 +0100 Subject: [PATCH] Enable single thread mode for runner Some runners for tools that can not be run in parallel need a way to specify that they should not be executed in parallel. This commits adds this functionality. Closes #5706 --- CHANGELOG.md | 3 + docs/Configuration.md | 4 ++ .../src/__tests__/test_scheduler.test.js | 57 +++++++++++++++++++ packages/jest-cli/src/test_scheduler.js | 2 +- 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 79bf4ef86472..fd78c73d1236 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,9 @@ ([#5670](https://github.com/facebook/jest/pull/5670)) * `[expect]` Add inverse matchers (`expect.not.arrayContaining`, etc., [#5517](https://github.com/facebook/jest/pull/5517)) +* `[jest-cli]` Add `isSerial` property that runners can expose to specify that + they can not run in parallel + [#5706](https://github.com/facebook/jest/pull/5706) ### Fixes diff --git a/docs/Configuration.md b/docs/Configuration.md index dc70b54fd4f6..0629190f3f9f 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -703,6 +703,10 @@ async runTests( ): Promise ``` +If you need to restrict your test-runner to only run in serial rather then being +executed in parallel your class should have the property `isSerial` to be +set as `true`. + ### `setupFiles` [array] Default: `[]` diff --git a/packages/jest-cli/src/__tests__/test_scheduler.test.js b/packages/jest-cli/src/__tests__/test_scheduler.test.js index 352e62a62771..25a43f02aad2 100644 --- a/packages/jest-cli/src/__tests__/test_scheduler.test.js +++ b/packages/jest-cli/src/__tests__/test_scheduler.test.js @@ -12,6 +12,20 @@ import TestScheduler from '../test_scheduler'; import SummaryReporter from '../reporters/summary_reporter'; jest.mock('../reporters/default_reporter'); +const mockSerialRunner = { + isSerial: true, + runTests: jest.fn(), +}; +jest.mock('jest-runner-serial', () => jest.fn(() => mockSerialRunner), { + virtual: true, +}); + +const mockParallelRunner = { + runTests: jest.fn(), +}; +jest.mock('jest-runner-parallel', () => jest.fn(() => mockParallelRunner), { + virtual: true, +}); test('.addReporter() .removeReporter()', () => { const scheduler = new TestScheduler({}, {}); @@ -21,3 +35,46 @@ test('.addReporter() .removeReporter()', () => { scheduler.removeReporter(SummaryReporter); expect(scheduler._dispatcher._reporters).not.toContain(reporter); }); + +test('schedule tests run in parallel per default', async () => { + const scheduler = new TestScheduler({}, {}); + const test = { + context: { + config: { + runner: 'jest-runner-parallel', + }, + }, + path: './test/path.js', + }; + const tests = [test, test]; + try { + await scheduler.scheduleTests(tests); + } catch (e) { + // Fails because a haste map is needed, irrelevant for the test + } + + expect(mockParallelRunner.runTests).toHaveBeenCalled(); + expect(mockParallelRunner.runTests.mock.calls[0][5].serial).toBeFalsy(); +}); + +test('schedule tests run in serial if the runner flags them', async () => { + const scheduler = new TestScheduler({}, {}); + const test = { + context: { + config: { + runner: 'jest-runner-serial', + }, + }, + path: './test/path.js', + }; + + const tests = [test, test]; + try { + await scheduler.scheduleTests(tests); + } catch (e) { + // Fails because a haste map is needed, irrelevant for the test + } + + expect(mockSerialRunner.runTests).toHaveBeenCalled(); + expect(mockSerialRunner.runTests.mock.calls[0][5].serial).toBeTruthy(); +}); diff --git a/packages/jest-cli/src/test_scheduler.js b/packages/jest-cli/src/test_scheduler.js index 2f7ec55002c0..3d5fb7d18064 100644 --- a/packages/jest-cli/src/test_scheduler.js +++ b/packages/jest-cli/src/test_scheduler.js @@ -192,7 +192,7 @@ export default class TestScheduler { onResult, onFailure, { - serial: runInBand, + serial: runInBand || testRunners[runner].isSerial, }, ); }