Skip to content

Commit 5a07f5f

Browse files
DanielMSchmidtcpojer
authored andcommitted
Enable single thread mode for runner (#5712)
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
1 parent 3e82577 commit 5a07f5f

4 files changed

Lines changed: 64 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
([#5670](https://github.com/facebook/jest/pull/5670))
1717
* `[expect]` Add inverse matchers (`expect.not.arrayContaining`, etc.,
1818
[#5517](https://github.com/facebook/jest/pull/5517))
19+
* `[jest-cli]` Add `isSerial` property that runners can expose to specify that
20+
they can not run in parallel
21+
[#5706](https://github.com/facebook/jest/pull/5706)
1922

2023
### Fixes
2124

docs/Configuration.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,10 @@ async runTests(
703703
): Promise<void>
704704
```
705705

706+
If you need to restrict your test-runner to only run in serial rather then being
707+
executed in parallel your class should have the property `isSerial` to be set as
708+
`true`.
709+
706710
### `setupFiles` [array]
707711

708712
Default: `[]`

packages/jest-cli/src/__tests__/test_scheduler.test.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ import TestScheduler from '../test_scheduler';
1212
import SummaryReporter from '../reporters/summary_reporter';
1313

1414
jest.mock('../reporters/default_reporter');
15+
const mockSerialRunner = {
16+
isSerial: true,
17+
runTests: jest.fn(),
18+
};
19+
jest.mock('jest-runner-serial', () => jest.fn(() => mockSerialRunner), {
20+
virtual: true,
21+
});
22+
23+
const mockParallelRunner = {
24+
runTests: jest.fn(),
25+
};
26+
jest.mock('jest-runner-parallel', () => jest.fn(() => mockParallelRunner), {
27+
virtual: true,
28+
});
1529

1630
test('.addReporter() .removeReporter()', () => {
1731
const scheduler = new TestScheduler({}, {});
@@ -21,3 +35,45 @@ test('.addReporter() .removeReporter()', () => {
2135
scheduler.removeReporter(SummaryReporter);
2236
expect(scheduler._dispatcher._reporters).not.toContain(reporter);
2337
});
38+
39+
test('schedule tests run in parallel per default', async () => {
40+
const scheduler = new TestScheduler({}, {});
41+
const test = {
42+
context: {
43+
config: {
44+
runner: 'jest-runner-parallel',
45+
},
46+
hasteFS: {
47+
matchFiles: jest.fn(() => []),
48+
},
49+
},
50+
path: './test/path.js',
51+
};
52+
const tests = [test, test];
53+
54+
await scheduler.scheduleTests(tests, {isInterrupted: jest.fn()});
55+
56+
expect(mockParallelRunner.runTests).toHaveBeenCalled();
57+
expect(mockParallelRunner.runTests.mock.calls[0][5].serial).toBeFalsy();
58+
});
59+
60+
test('schedule tests run in serial if the runner flags them', async () => {
61+
const scheduler = new TestScheduler({}, {});
62+
const test = {
63+
context: {
64+
config: {
65+
runner: 'jest-runner-serial',
66+
},
67+
hasteFS: {
68+
matchFiles: jest.fn(() => []),
69+
},
70+
},
71+
path: './test/path.js',
72+
};
73+
74+
const tests = [test, test];
75+
await scheduler.scheduleTests(tests, {isInterrupted: jest.fn()});
76+
77+
expect(mockSerialRunner.runTests).toHaveBeenCalled();
78+
expect(mockSerialRunner.runTests.mock.calls[0][5].serial).toBeTruthy();
79+
});

packages/jest-cli/src/test_scheduler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ export default class TestScheduler {
192192
onResult,
193193
onFailure,
194194
{
195-
serial: runInBand,
195+
serial: runInBand || Boolean(testRunners[runner].isSerial),
196196
},
197197
);
198198
}

0 commit comments

Comments
 (0)