From 5a57c1a9f6c2e701b4e260f13328ea035df44d38 Mon Sep 17 00:00:00 2001 From: Dmitrii Abramov Date: Mon, 10 Jul 2017 14:39:27 -0700 Subject: [PATCH] add maxWorkers to globalConfig --- .../__snapshots__/custom_reporters.test.js.snap | 3 --- .../__tests__/__snapshots__/show_config.test.js.snap | 1 + integration_tests/__tests__/show_config.test.js | 1 + integration_tests/__tests__/snapshot.test.js | 3 +++ packages/jest-cli/src/__tests__/test_runner.test.js | 8 ++++---- packages/jest-cli/src/cli/index.js | 3 +-- packages/jest-cli/src/run_jest.js | 3 --- packages/jest-cli/src/test_runner.js | 11 +++++------ .../src}/__tests__/get_max_workers.test.js | 0 .../src/lib => jest-config/src}/get_max_workers.js | 0 packages/jest-config/src/index.js | 1 + packages/jest-config/src/normalize.js | 3 +++ types/Config.js | 1 + 13 files changed, 20 insertions(+), 18 deletions(-) rename packages/{jest-cli/src/lib => jest-config/src}/__tests__/get_max_workers.test.js (100%) rename packages/{jest-cli/src/lib => jest-config/src}/get_max_workers.js (100%) diff --git a/integration_tests/__tests__/__snapshots__/custom_reporters.test.js.snap b/integration_tests/__tests__/__snapshots__/custom_reporters.test.js.snap index eba40a73f12c..c819eff815a9 100644 --- a/integration_tests/__tests__/__snapshots__/custom_reporters.test.js.snap +++ b/integration_tests/__tests__/__snapshots__/custom_reporters.test.js.snap @@ -32,7 +32,6 @@ Object { "christoph": "pojer", "dmitrii": "abramov", "hello": "world", - "maxWorkers": "<>", "pattern": Object { "input": "add_fail.test.js", "paths": Array [ @@ -70,7 +69,6 @@ Object { "christoph": "pojer", "dmitrii": "abramov", "hello": "world", - "maxWorkers": "<>", "pattern": Object { "input": "add.test.js", "paths": Array [ @@ -122,7 +120,6 @@ Object { "path": false, }, "options": Object { - "maxWorkers": "<>", "pattern": Object { "input": "add.test.js", "paths": Array [ diff --git a/integration_tests/__tests__/__snapshots__/show_config.test.js.snap b/integration_tests/__tests__/__snapshots__/show_config.test.js.snap index 47c3fb62cbc4..46f7ba0874f7 100644 --- a/integration_tests/__tests__/__snapshots__/show_config.test.js.snap +++ b/integration_tests/__tests__/__snapshots__/show_config.test.js.snap @@ -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", diff --git a/integration_tests/__tests__/show_config.test.js b/integration_tests/__tests__/show_config.test.js index 959fa08ce27c..fb7d0bf67949 100644 --- a/integration_tests/__tests__/show_config.test.js +++ b/integration_tests/__tests__/show_config.test.js @@ -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', }); diff --git a/integration_tests/__tests__/snapshot.test.js b/integration_tests/__tests__/snapshot.test.js index d53faf06a215..7c0c25b36587 100644 --- a/integration_tests/__tests__/snapshot.test.js +++ b/integration_tests/__tests__/snapshot.test.js @@ -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, diff --git a/packages/jest-cli/src/__tests__/test_runner.test.js b/packages/jest-cli/src/__tests__/test_runner.test.js index 7cba6bf20eee..79d4f17516f3 100644 --- a/packages/jest-cli/src/__tests__/test_runner.test.js +++ b/packages/jest-cli/src/__tests__/test_runner.test.js @@ -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( @@ -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( diff --git a/packages/jest-cli/src/cli/index.js b/packages/jest-cli/src/cli/index.js index c27b9fa7a149..15c05a384be9 100644 --- a/packages/jest-cli/src/cli/index.js +++ b/packages/jest-cli/src/cli/index.js @@ -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'; @@ -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, diff --git a/packages/jest-cli/src/run_jest.js b/packages/jest-cli/src/run_jest.js index 357b0f31819a..10d169cddba3 100644 --- a/packages/jest-cli/src/run_jest.js +++ b/packages/jest-cli/src/run_jest.js @@ -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'; @@ -178,7 +177,6 @@ const runJest = async ( testSelectionConfig: TestSelectionConfig, ) => void, ) => { - const maxWorkers = getMaxWorkers(argv); const testSelectionConfig = getTestSelectionConfig(argv); const sequencer = new TestSequencer(); let allTests = []; @@ -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, diff --git a/packages/jest-cli/src/test_runner.js b/packages/jest-cli/src/test_runner.js index 74c3d82f81a4..66398461ddec 100644 --- a/packages/jest-cli/src/test_runner.js +++ b/packages/jest-cli/src/test_runner.js @@ -47,7 +47,6 @@ class CancelRun extends Error { } export type TestRunnerOptions = {| - maxWorkers: number, pattern: TestSelectionConfig, startRun: () => *, testNamePattern: string, @@ -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 && @@ -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 @@ -309,7 +308,7 @@ class TestRunner { if (collectCoverage) { this.addReporter( new CoverageReporter(this._globalConfig, { - maxWorkers: this._options.maxWorkers, + maxWorkers: this._globalConfig.maxWorkers, }), ); } diff --git a/packages/jest-cli/src/lib/__tests__/get_max_workers.test.js b/packages/jest-config/src/__tests__/get_max_workers.test.js similarity index 100% rename from packages/jest-cli/src/lib/__tests__/get_max_workers.test.js rename to packages/jest-config/src/__tests__/get_max_workers.test.js diff --git a/packages/jest-cli/src/lib/get_max_workers.js b/packages/jest-config/src/get_max_workers.js similarity index 100% rename from packages/jest-cli/src/lib/get_max_workers.js rename to packages/jest-config/src/get_max_workers.js diff --git a/packages/jest-config/src/index.js b/packages/jest-config/src/index.js index 7cf2ab1fbb5b..8576796dd801 100644 --- a/packages/jest-config/src/index.js +++ b/packages/jest-config/src/index.js @@ -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, diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index 85575038b577..a9430c2dbafa 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -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'; @@ -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', diff --git a/types/Config.js b/types/Config.js index 2bbc23df5729..5f1a413e1051 100644 --- a/types/Config.js +++ b/types/Config.js @@ -137,6 +137,7 @@ export type GlobalConfig = {| forceExit: boolean, logHeapUsage: boolean, mapCoverage: boolean, + maxWorkers: number, noStackTrace: boolean, notify: boolean, projects: Array,