Skip to content

Commit

Permalink
add maxWorkers to globalConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronabramov committed Jul 11, 2017
1 parent 9d8eb23 commit eb42f02
Show file tree
Hide file tree
Showing 13 changed files with 61 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
43 changes: 43 additions & 0 deletions integration_tests/snapshot/__tests__/snapshot.test_copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* 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';

describe('snapshot', () => {
it('works with plain objects and the title has `escape` characters', () => {
const test = {
a: 1,
b: '2',
c: 'three`',
};
expect(test).toMatchSnapshot();
test.d = '4';
expect(test).toMatchSnapshot();
});

it('is not influenced by previous counter', () => {
const test = {
a: 43,
b: '43',
c: 'fourtythree',
};
expect(test).toMatchSnapshot();
});

it('cannot be used with .not', () => {
expect(() => expect('').not.toMatchSnapshot()).toThrow(
'Jest: `.not` cannot be used with `.toMatchSnapshot()`.'
);
});

// Issue reported here: https://github.com/facebook/jest/issues/2969
it('works with \\r\\n', () => {
expect('<div>\r\n</div>').toMatchSnapshot();
});
});
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
12 changes: 6 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 @@ -205,6 +204,7 @@ class TestRunner {
}

this._dispatcher.onTestStart(test);
console.error(test.path);
return runTest(
test.path,
this._globalConfig,
Expand All @@ -229,12 +229,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 +309,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 eb42f02

Please sign in to comment.