Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract watch mode #2362

Merged
merged 17 commits into from Jan 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/jest-cli/src/TestRunner.js
Expand Up @@ -328,14 +328,22 @@ class TestRunner {
maxRetries: 2, // Allow for a couple of transient errors.
}, TEST_WORKER_PATH);
const mutex = throat(this._options.maxWorkers);
const worker = promisify(farm);

// Send test suites to workers continuously instead of all at once to track
// the start time of individual tests.
const runTestInWorker = ({path, config}) => mutex(() => {
const runTestInWorker = ({config, path}) => mutex(() => {
if (watcher.isInterrupted()) {
return Promise.reject();
}
this._dispatcher.onTestStart(config, path);
return promisify(farm)({config, path});
return worker({
config,
path,
rawModuleMap: watcher.isWatchMode()
? this._hasteContext.moduleMap.getRawModuleMap()
: null,
});
});

const onError = (err, path) => {
Expand Down
41 changes: 30 additions & 11 deletions packages/jest-cli/src/TestWorker.js
Expand Up @@ -11,22 +11,25 @@

import type {Config, Path} from 'types/Config';
import type {Error, TestResult} from 'types/TestResult';

const {separateMessageFromStack} = require('jest-util');
import type {RawModuleMap} from 'types/HasteMap';

// Make sure uncaught errors are logged before we exit.
process.on('uncaughtException', err => {
console.error(err.stack);
process.exit(1);
});

const {ModuleMap} = require('jest-haste-map');
const {separateMessageFromStack} = require('jest-util');

const Runtime = require('jest-runtime');
const runTest = require('./runTest');

type WorkerData = {
type WorkerData = {|
config: Config,
path: Path,
};
rawModuleMap?: RawModuleMap,
|};

type WorkerCallback = (error: ?Error, result?: TestResult) => void;

Expand All @@ -48,18 +51,34 @@ const formatError = error => {
};

const resolvers = Object.create(null);

module.exports = (data: WorkerData, callback: WorkerCallback) => {
try {
const name = data.config.name;
const getResolver = (config, rawModuleMap) => {
// In watch mode, the raw module map with all haste modules is passed from
// the test runner to the watch command. This is because jest-haste-map's
// watch mode does not persist the haste map on disk after every file change.
// To make this fast and consistent, we pass it from the TestRunner.
if (rawModuleMap) {
return Runtime.createResolver(
config,
new ModuleMap(rawModuleMap.map, rawModuleMap.mocks),
);
} else {
const name = config.name;
if (!resolvers[name]) {
resolvers[name] = Runtime.createResolver(
data.config,
Runtime.createHasteMap(data.config).readModuleMap(),
config,
Runtime.createHasteMap(config).readModuleMap(),
);
}
return resolvers[name];
}
};

runTest(data.path, data.config, resolvers[name])
module.exports = (
{config, path, rawModuleMap}: WorkerData,
callback: WorkerCallback,
) => {
try {
runTest(path, config, getResolver(config, rawModuleMap))
.then(
result => callback(null, result),
error => callback(formatError(error)),
Expand Down
60 changes: 60 additions & 0 deletions packages/jest-cli/src/__tests__/TestRunner-test.js
Expand Up @@ -11,8 +11,24 @@
'use strict';

const TestRunner = require('../TestRunner');
const TestWatcher = require('../TestWatcher');
const SummaryReporter = require('../reporters/SummaryReporter');

let workerFarmMock;

jest.mock('worker-farm', () => {
const mock = jest.fn(
(options, worker) => workerFarmMock = jest.fn(
(data, callback) => require(worker)(data, callback),
),
);
mock.end = jest.fn();
return mock;
});

jest.mock('../TestWorker', () => {});
jest.mock('../reporters/DefaultReporter');

test('.addReporter() .removeReporter()', () => {
const runner = new TestRunner({}, {}, {});
const reporter = new SummaryReporter();
Expand All @@ -21,3 +37,47 @@ test('.addReporter() .removeReporter()', () => {
runner.removeReporter(SummaryReporter);
expect(runner._dispatcher._reporters).not.toContain(reporter);
});


describe('_createInBandTestRun()', () => {
test('injects the rawModuleMap to each the worker in watch mode', () => {
const config = {watch: true};
const rawModuleMap = jest.fn();
const hasteContext = {moduleMap: {getRawModuleMap: () => rawModuleMap}};

const runner = new TestRunner(hasteContext, config, {maxWorkers: 2});

return runner._createParallelTestRun(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to tests _createParallelTestRun instead of runTests even though it is a private method.

I did this because the current implementation of runTests is a harder to test, mainly because of all of the extra logic that the inner callbacks have. If you prefer I can also spend some time refactoring some of it to make runTests easier to test :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I was thinking that as well. We could do that as a follow-up. TestRunner is one of the oldest parts of Jest and has been through a lot. I'm open to any ideas on how to improve the code to make it more testable. Feel free to create an issues to discuss or a PR to fix.

One thing that is untested as well is the performance test cache and that Jest runs failed tests first on a re-run. This all may break at any time. I'm really grateful you are adding such extensive unit tests for the watch mode, as it was entirely untested previously.

['./file-test.js', './file2-test.js'],
new TestWatcher({isWatchMode: config.watch}),
() => {},
() => {},
).then(() => {
expect(workerFarmMock.mock.calls).toEqual([
[{config, path: './file-test.js', rawModuleMap}, jasmine.any(Function)],
// eslint-disable-next-line max-len
[{config, path: './file2-test.js', rawModuleMap}, jasmine.any(Function)],
]);
});
});

test('does not inject the rawModuleMap in non watch mode', () => {
const config = {watch: false};

const runner = new TestRunner({}, config, {maxWorkers: 1});

return runner._createParallelTestRun(
['./file-test.js', './file2-test.js'],
new TestWatcher({isWatchMode: config.watch}),
() => {},
() => {},
).then(() => {
expect(workerFarmMock.mock.calls).toEqual([
/* eslint-disable max-len */
[{config, path: './file-test.js', rawModuleMap: null}, jasmine.any(Function)],
[{config, path: './file2-test.js', rawModuleMap: null}, jasmine.any(Function)],
/* eslint-enable max-len */
]);
});
});
});
158 changes: 158 additions & 0 deletions packages/jest-cli/src/__tests__/watch-test.js
@@ -0,0 +1,158 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails oncall+jsinfra
*/

'use strict';

const chalk = require('chalk');
const TestWatcher = require('../TestWatcher');
const {KEYS} = require('../constants');

const runJestMock = jest.fn();

jest.mock('jest-util', () => ({clearLine: () => {}}));
jest.doMock('chalk', () => new chalk.constructor({enabled: false}));
jest.doMock('../constants', () => ({CLEAR: '', KEYS}));
jest.doMock('../runJest', () => (...args) => {
runJestMock(...args);

// Call the callback
args[args.length - 1]({snapshot: {}});

return Promise.resolve();
});

const watch = require('../watch');

const USAGE_MESSAGE = `
Watch Usage
› Press o to only run tests related to changed files.
› Press p to filter by a filename regex pattern.
› Press q to quit watch mode.
› Press Enter to trigger a test run.`;

afterEach(runJestMock.mockReset);

describe('Watch mode flows', () => {
let pipe;
let hasteMap;
let argv;
let hasteContext;
let config;
let stdin;

beforeEach(() => {
pipe = {write: jest.fn()};
hasteMap = {on: () => {}};
argv = {};
hasteContext = {};
config = {};
stdin = new MockStdin();
});

it('Runs Jest once by default and shows usage', () => {
watch(config, pipe, argv, hasteMap, hasteContext, stdin);
expect(runJestMock).toBeCalledWith(hasteContext, config, argv, pipe,
new TestWatcher({isWatchMode: true}), jasmine.any(Function));
expect(pipe.write).toBeCalledWith(USAGE_MESSAGE);
});

it('Pressing "o" runs test in "only changed files" mode', () => {
watch(config, pipe, argv, hasteMap, hasteContext, stdin);
runJestMock.mockReset();

stdin.emit(KEYS.O);

expect(runJestMock).toBeCalled();
expect(argv).toEqual({
'_': '',
onlyChanged: true,
watch: true,
watchAll: false,
});
});

it('Pressing "a" runs test in "watch all" mode', () => {
watch(config, pipe, argv, hasteMap, hasteContext, stdin);
runJestMock.mockReset();

stdin.emit(KEYS.A);

expect(runJestMock).toBeCalled();
expect(argv).toEqual({
'_': '',
onlyChanged: false,
watch: false,
watchAll: true,
});
});

it('Pressing "P" enters pattern mode', () => {
watch(config, pipe, argv, hasteMap, hasteContext, stdin);

// Write a enter pattern mode
stdin.emit(KEYS.P);
expect(pipe.write).toBeCalledWith(' pattern › ');

// Write a pattern
stdin.emit(KEYS.P);
stdin.emit(KEYS.O);
stdin.emit(KEYS.A);
expect(pipe.write).toBeCalledWith(' pattern › poa');

//Runs Jest again
runJestMock.mockReset();
stdin.emit(KEYS.ENTER);
expect(runJestMock).toBeCalled();

//Argv is updated with the current pattern
expect(argv).toEqual({
'_': ['poa'],
onlyChanged: false,
watch: true,
watchAll: false,
});
});

it('Pressing "ENTER" reruns the tests', () => {
watch(config, pipe, argv, hasteMap, hasteContext, stdin);
expect(runJestMock).toHaveBeenCalledTimes(1);
stdin.emit(KEYS.ENTER);
expect(runJestMock).toHaveBeenCalledTimes(2);
});

it('Pressing "u" reruns the tests in "update snapshot" mode', () => {
watch(config, pipe, argv, hasteMap, hasteContext, stdin);
runJestMock.mockReset();

stdin.emit(KEYS.U);

expect(runJestMock.mock.calls[0][1]).toEqual({updateSnapshot: true});
});
});

class MockStdin {
constructor() {
this._callbacks = [];
}

setRawMode() {}

resume() {}

setEncoding() {}

on(evt, callback) {
this._callbacks.push(callback);
}

emit(key) {
this._callbacks.forEach(cb => cb(key));
}
}
33 changes: 33 additions & 0 deletions packages/jest-cli/src/constants.js
@@ -0,0 +1,33 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/

'use strict';

const CLEAR = process.platform === 'win32' ? '\x1Bc' : '\x1B[2J\x1B[3J\x1B[H';

const KEYS = {
A: '61',
ARROW_DOWN: '1b5b42',
ARROW_LEFT: '1b5b44',
ARROW_RIGHT: '1b5b43',
ARROW_UP: '1b5b41',
BACKSPACE: process.platform === 'win32' ? '08' : '7f',
CONTROL_C: '03',
CONTROL_D: '04',
ENTER: '0d',
ESCAPE: '1b',
O: '6f',
P: '70',
Q: '71',
QUESTION_MARK: '3f',
U: '75',
};

module.exports = {CLEAR, KEYS};