Skip to content

Commit

Permalink
add maxWorkers to globalConfig (#4005)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronabramov authored Jul 12, 2017
1 parent d58e466 commit dd6c5c4
Show file tree
Hide file tree
Showing 13 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ Object {
"christoph": "pojer",
"dmitrii": "abramov",
"hello": "world",
"maxWorkers": "<<REPLACED>>",
"pattern": Object {
"input": "add_fail.test.js",
"paths": Array [
Expand Down Expand Up @@ -70,7 +69,6 @@ Object {
"christoph": "pojer",
"dmitrii": "abramov",
"hello": "world",
"maxWorkers": "<<REPLACED>>",
"pattern": Object {
"input": "add.test.js",
"paths": Array [
Expand Down Expand Up @@ -122,7 +120,6 @@ Object {
"path": false,
},
"options": Object {
"maxWorkers": "<<REPLACED>>",
"pattern": Object {
"input": "add.test.js",
"paths": Array [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ exports[`jest --showConfig outputs config info and exits 1`] = `
],
"expand": false,
"mapCoverage": false,
"maxWorkers": "[maxWorkers]",
"noStackTrace": false,
"notify": false,
"rootDir": "/mocked/root/path/jest/integration_tests/verbose_reporter",
Expand Down
1 change: 1 addition & 0 deletions integration_tests/__tests__/show_config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('jest --showConfig', () => {
.replace(/"cacheDirectory": "(.+)"/g, '"cacheDirectory": "/tmp/jest"')
.replace(/"name": "(.+)"/g, '"name": "[md5 hash]"')
.replace(/"version": "(.+)"/g, '"version": "[version]"')
.replace(/"maxWorkers": (\d+)/g, '"maxWorkers": "[maxWorkers]"')
.replace(new RegExp(root, 'g'), '/mocked/root/path'),
test: val => typeof val === 'string',
});
Expand Down
3 changes: 3 additions & 0 deletions integration_tests/__tests__/snapshot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const path = require('path');
const {extractSummary} = require('../utils');
const runJest = require('../runJest');

const skipOnWindows = require('skipOnWindows');
skipOnWindows.suite();

const emptyTest = 'describe("", () => {it("", () => {})})';
const snapshotDir = path.resolve(
__dirname,
Expand Down
8 changes: 4 additions & 4 deletions packages/jest-cli/src/__tests__/test_runner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ test('.addReporter() .removeReporter()', () => {

describe('_createInBandTestRun()', () => {
test('injects the rawModuleMap to each the worker in watch mode', () => {
const globalConfig = {watch: true};
const globalConfig = {maxWorkers: 2, watch: true};
const config = {rootDir: '/path/'};
const rawModuleMap = jest.fn();
const context = {
config,
moduleMap: {getRawModuleMap: () => rawModuleMap},
};
const runner = new TestRunner(globalConfig, {maxWorkers: 2});
const runner = new TestRunner(globalConfig, {});

return runner
._createParallelTestRun(
Expand All @@ -72,10 +72,10 @@ describe('_createInBandTestRun()', () => {
});

test('does not inject the rawModuleMap in non watch mode', () => {
const globalConfig = {watch: false};
const globalConfig = {maxWorkers: 1, watch: false};
const config = {rootDir: '/path/'};
const context = {config};
const runner = new TestRunner(globalConfig, {maxWorkers: 1});
const runner = new TestRunner(globalConfig, {});

return runner
._createParallelTestRun(
Expand Down
3 changes: 1 addition & 2 deletions packages/jest-cli/src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import chalk from 'chalk';
import createContext from '../lib/create_context';
import getChangedFilesPromise from '../get_changed_files_promise';
import getJest from './get_jest';
import getMaxWorkers from '../lib/get_max_workers';
import handleDeprecationWarnings from '../lib/handle_deprecation_warnings';
import logDebugMessages from '../lib/log_debug_messages';
import preRunMessage from '../pre_run_message';
Expand Down Expand Up @@ -231,7 +230,7 @@ const _buildContextsAndHasteMaps = async (
createDirectory(config.cacheDirectory);
const hasteMapInstance = Runtime.createHasteMap(config, {
console: new Console(outputStream, outputStream),
maxWorkers: getMaxWorkers(argv),
maxWorkers: globalConfig.maxWorkers,
resetCache: !config.cache,
watch: globalConfig.watch,
watchman: globalConfig.watchman,
Expand Down
3 changes: 0 additions & 3 deletions packages/jest-cli/src/run_jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import path from 'path';
import {Console, formatTestResults} from 'jest-util';
import chalk from 'chalk';
import fs from 'graceful-fs';
import getMaxWorkers from './lib/get_max_workers';
import getTestSelectionConfig from './lib/get_test_selection_config';
import SearchSource from './search_source';
import updateArgv from './lib/update_argv';
Expand Down Expand Up @@ -178,7 +177,6 @@ const runJest = async (
testSelectionConfig: TestSelectionConfig,
) => void,
) => {
const maxWorkers = getMaxWorkers(argv);
const testSelectionConfig = getTestSelectionConfig(argv);
const sequencer = new TestSequencer();
let allTests = [];
Expand Down Expand Up @@ -231,7 +229,6 @@ const runJest = async (
setConfig(contexts, {rootDir: process.cwd()});

const results = await new TestRunner(globalConfig, {
maxWorkers,
pattern: testSelectionConfig,
startRun,
testNamePattern: argv.testNamePattern,
Expand Down
11 changes: 5 additions & 6 deletions packages/jest-cli/src/test_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class CancelRun extends Error {
}

export type TestRunnerOptions = {|
maxWorkers: number,
pattern: TestSelectionConfig,
startRun: () => *,
testNamePattern: string,
Expand Down Expand Up @@ -91,14 +90,14 @@ class TestRunner {

const aggregatedResults = createAggregatedResults(tests.length);
const estimatedTime = Math.ceil(
getEstimatedTime(timings, this._options.maxWorkers) / 1000,
getEstimatedTime(timings, this._globalConfig.maxWorkers) / 1000,
);

// Run in band if we only have one test or one worker available.
// If we are confident from previous runs that the tests will finish quickly
// we also run in band to reduce the overhead of spawning workers.
const runInBand =
this._options.maxWorkers <= 1 ||
this._globalConfig.maxWorkers <= 1 ||
tests.length <= 1 ||
(tests.length <= 20 &&
timings.length > 0 &&
Expand Down Expand Up @@ -229,12 +228,12 @@ class TestRunner {
{
autoStart: true,
maxConcurrentCallsPerWorker: 1,
maxConcurrentWorkers: this._options.maxWorkers,
maxConcurrentWorkers: this._globalConfig.maxWorkers,
maxRetries: 2, // Allow for a couple of transient errors.
},
TEST_WORKER_PATH,
);
const mutex = throat(this._options.maxWorkers);
const mutex = throat(this._globalConfig.maxWorkers);
const worker = pify(farm);

// Send test suites to workers continuously instead of all at once to track
Expand Down Expand Up @@ -309,7 +308,7 @@ class TestRunner {
if (collectCoverage) {
this.addReporter(
new CoverageReporter(this._globalConfig, {
maxWorkers: this._options.maxWorkers,
maxWorkers: this._globalConfig.maxWorkers,
}),
);
}
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions packages/jest-config/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const getConfigs = (
forceExit: options.forceExit,
logHeapUsage: options.logHeapUsage,
mapCoverage: options.mapCoverage,
maxWorkers: options.maxWorkers,
noStackTrace: options.noStackTrace,
notify: options.notify,
projects: options.projects,
Expand Down
3 changes: 3 additions & 0 deletions packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import crypto from 'crypto';
import path from 'path';
import {ValidationError, validate} from 'jest-validate';
import chalk from 'chalk';
import getMaxWorkers from './get_max_workers';
import glob from 'glob';
import Resolver from 'jest-resolve';
import utils from 'jest-regex-util';
Expand Down Expand Up @@ -452,6 +453,8 @@ function normalize(options: InitialOptions, argv: Argv) {
? 'none'
: argv.updateSnapshot ? 'all' : 'new';

newOptions.maxWorkers = getMaxWorkers(argv);

if (babelJest) {
const regeneratorRuntimePath = Resolver.findNodeModule(
'regenerator-runtime/runtime',
Expand Down
1 change: 1 addition & 0 deletions types/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export type GlobalConfig = {|
forceExit: boolean,
logHeapUsage: boolean,
mapCoverage: boolean,
maxWorkers: number,
noStackTrace: boolean,
notify: boolean,
projects: Array<Glob>,
Expand Down

0 comments on commit dd6c5c4

Please sign in to comment.