Skip to content

Commit

Permalink
wip: mock event listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
sauravhiremath committed Dec 25, 2020
1 parent c3c3fc1 commit 5d68ded
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions packages/jest-core/src/__tests__/TestScheduler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,23 @@ import TestScheduler from '../TestScheduler';
import * as testSchedulerHelper from '../testSchedulerHelper';

jest.mock('@jest/reporters');
let events = {};

const mockSerialRunner = {
isSerial: true,
on: jest.fn((eventName, callback) => {
events[eventName] = callback;
}),
runTests: jest.fn(),
};
jest.mock('jest-runner-serial', () => jest.fn(() => mockSerialRunner), {
virtual: true,
});

const mockParallelRunner = {
on: jest.fn((eventName, callback) => {
events[eventName] = callback;
}),
runTests: jest.fn(),
};
jest.mock('jest-runner-parallel', () => jest.fn(() => mockParallelRunner), {
Expand All @@ -30,8 +38,11 @@ jest.mock('jest-runner-parallel', () => jest.fn(() => mockParallelRunner), {
const spyShouldRunInBand = jest.spyOn(testSchedulerHelper, 'shouldRunInBand');

beforeEach(() => {
events = {};
mockSerialRunner.runTests.mockClear();
mockSerialRunner.on.mockClear();
mockParallelRunner.runTests.mockClear();
mockParallelRunner.on.mockClear();
spyShouldRunInBand.mockClear();
});

Expand Down Expand Up @@ -103,7 +114,7 @@ test('schedule tests run in parallel per default', async () => {
await scheduler.scheduleTests(tests, {isInterrupted: jest.fn()});

expect(mockParallelRunner.runTests).toHaveBeenCalled();
expect(mockParallelRunner.runTests.mock.calls[0][5].serial).toBeFalsy();
expect(mockParallelRunner.runTests.mock.calls[0][2].serial).toBeFalsy();
});

test('schedule tests run in serial if the runner flags them', async () => {
Expand All @@ -126,7 +137,7 @@ test('schedule tests run in serial if the runner flags them', async () => {
await scheduler.scheduleTests(tests, {isInterrupted: jest.fn()});

expect(mockSerialRunner.runTests).toHaveBeenCalled();
expect(mockSerialRunner.runTests.mock.calls[0][5].serial).toBeTruthy();
expect(mockSerialRunner.runTests.mock.calls[0][2].serial).toBeTruthy();
});

test('should bail after `n` failures', async () => {
Expand All @@ -153,7 +164,7 @@ test('should bail after `n` failures', async () => {
isWatchMode: () => true,
setState,
});
await mockSerialRunner.runTests.mock.calls[0][3](test, {
await mockSerialRunner.runTests(tests, {
numFailingTests: 2,
snapshot: {},
testResults: [{}],
Expand Down Expand Up @@ -185,11 +196,6 @@ test('should not bail if less than `n` failures', async () => {
isWatchMode: () => true,
setState,
});
await mockSerialRunner.runTests.mock.calls[0][3](test, {
numFailingTests: 1,
snapshot: {},
testResults: [{}],
});
expect(setState).not.toBeCalled();
});

Expand All @@ -216,7 +222,7 @@ test('should set runInBand to run in serial', async () => {

expect(spyShouldRunInBand).toHaveBeenCalled();
expect(mockParallelRunner.runTests).toHaveBeenCalled();
expect(mockParallelRunner.runTests.mock.calls[0][5].serial).toBeTruthy();
expect(mockParallelRunner.runTests.mock.calls[0][2].serial).toBeTruthy();
});

test('should set runInBand to not run in serial', async () => {
Expand All @@ -242,5 +248,5 @@ test('should set runInBand to not run in serial', async () => {

expect(spyShouldRunInBand).toHaveBeenCalled();
expect(mockParallelRunner.runTests).toHaveBeenCalled();
expect(mockParallelRunner.runTests.mock.calls[0][5].serial).toBeFalsy();
expect(mockParallelRunner.runTests.mock.calls[0][2].serial).toBeFalsy();
});

0 comments on commit 5d68ded

Please sign in to comment.