Skip to content

Commit

Permalink
Enable single thread mode for runner (jestjs#5712)
Browse files Browse the repository at this point in the history
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 jestjs#5706
  • Loading branch information
DanielMSchmidt authored and thomasjinlo committed Mar 9, 2018
1 parent 3e82577 commit 2fc67dd
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,10 @@ async runTests(
): Promise<void>
```

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: `[]`
Expand Down
56 changes: 56 additions & 0 deletions packages/jest-cli/src/__tests__/test_scheduler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({}, {});
Expand All @@ -21,3 +35,45 @@ 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',
},
hasteFS: {
matchFiles: jest.fn(() => []),
},
},
path: './test/path.js',
};
const tests = [test, test];

await scheduler.scheduleTests(tests, {isInterrupted: jest.fn()});

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',
},
hasteFS: {
matchFiles: jest.fn(() => []),
},
},
path: './test/path.js',
};

const tests = [test, test];
await scheduler.scheduleTests(tests, {isInterrupted: jest.fn()});

expect(mockSerialRunner.runTests).toHaveBeenCalled();
expect(mockSerialRunner.runTests.mock.calls[0][5].serial).toBeTruthy();
});
2 changes: 1 addition & 1 deletion packages/jest-cli/src/test_scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export default class TestScheduler {
onResult,
onFailure,
{
serial: runInBand,
serial: runInBand || Boolean(testRunners[runner].isSerial),
},
);
}
Expand Down

0 comments on commit 2fc67dd

Please sign in to comment.