Skip to content

Commit

Permalink
Add —experimentalProjects to run multiple projects within the same …
Browse files Browse the repository at this point in the history
…jest-cli test run.
  • Loading branch information
cpojer committed Mar 17, 2017
1 parent 14e5175 commit 1a30f7c
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 130 deletions.
59 changes: 31 additions & 28 deletions packages/jest-cli/src/TestRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ class TestRunner {
}
});

const config = this._config;
const aggregatedResults = createAggregatedResults(tests.length);
const estimatedTime = Math.ceil(
getEstimatedTime(timings, this._options.maxWorkers) / 1000,
Expand Down Expand Up @@ -120,7 +119,7 @@ class TestRunner {
return;
}
addResult(aggregatedResults, testResult);
this._dispatcher.onTestResult(config, testResult, aggregatedResults);
this._dispatcher.onTestResult(test.config, testResult, aggregatedResults);
this._bailIfNeeded(aggregatedResults, watcher);
};

Expand All @@ -135,10 +134,11 @@ class TestRunner {
test.path,
);
addResult(aggregatedResults, testResult);
this._dispatcher.onTestResult(config, testResult, aggregatedResults);
this._dispatcher.onTestResult(test.config, testResult, aggregatedResults);
};

const updateSnapshotState = () => {
const config = this._config;
const status = snapshot.cleanup(
this._context.hasteFS,
config.updateSnapshot,
Expand All @@ -151,7 +151,7 @@ class TestRunner {
aggregatedResults.snapshot.filesRemoved));
};

this._dispatcher.onRunStart(config, aggregatedResults, {
this._dispatcher.onRunStart(this._config, aggregatedResults, {
estimatedTime,
showStatus: !runInBand,
});
Expand All @@ -169,7 +169,7 @@ class TestRunner {
updateSnapshotState();
aggregatedResults.wasInterrupted = watcher.isInterrupted();

this._dispatcher.onRunComplete(config, aggregatedResults);
this._dispatcher.onRunComplete(this._config, aggregatedResults);

const anyTestFailures = !(aggregatedResults.numFailedTests === 0 &&
aggregatedResults.numRuntimeErrorTestSuites === 0);
Expand All @@ -190,17 +190,19 @@ class TestRunner {
) {
const mutex = throat(1);
return tests.reduce(
(promise, test) => mutex(() => promise
.then(() => {
if (watcher.isInterrupted()) {
throw new CancelRun();
}

this._dispatcher.onTestStart(test.config, test.path);
return runTest(test.path, test.config, this._context.resolver);
})
.then(result => onResult(test, result))
.catch(err => onFailure(test, err))),
(promise, test) =>
mutex(() =>
promise
.then(() => {
if (watcher.isInterrupted()) {
throw new CancelRun();
}

this._dispatcher.onTestStart(test.config, test.path);
return runTest(test.path, test.config, this._context.resolver);
})
.then(result => onResult(test, result))
.catch(err => onFailure(test, err))),
Promise.resolve(),
);
}
Expand All @@ -225,19 +227,20 @@ class TestRunner {

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

const onError = (err, test) => {
onFailure(test, err);
Expand Down
5 changes: 5 additions & 0 deletions packages/jest-cli/src/cli/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ const options = {
description: 'Use this flag to show full diffs instead of a patch.',
type: 'boolean',
},
experimentalProjects: {
description: 'A list of projects that use Jest to run all tests in a ' +
'single run.',
type: 'array',
},
findRelatedTests: {
description: 'Find related tests for a list of source files that were ' +
'passed in as arguments. Useful for pre-commit hook integration to run ' +
Expand Down
8 changes: 7 additions & 1 deletion packages/jest-cli/src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ function run(argv?: Object, root?: Path) {
root = pkgDir.sync();
}

getJest(root).runCLI(argv, root, result => {
argv.projects = argv.experimentalProjects;
if (!argv.projects) {
argv.projects = [root];
}

const execute = argv.projects.length === 1 ? getJest(root).runCLI : runCLI;
execute(argv, argv.projects, result => {
const code = !result || result.success ? 0 : 1;
process.on('exit', () => process.exit(code));
if (argv && argv.forceExit) {
Expand Down
68 changes: 41 additions & 27 deletions packages/jest-cli/src/cli/runCLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'use strict';

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

const Runtime = require('jest-runtime');

Expand All @@ -28,9 +28,9 @@ const watch = require('../watch');

const VERSION = require('../../package.json').version;

module.exports = (
module.exports = async (
argv: Object,
root: Path,
roots: Array<Path>,
onComplete: (results: ?AggregatedResult) => void,
) => {
const realFs = require('fs');
Expand All @@ -45,52 +45,66 @@ module.exports = (
return;
}

const _run = async ({config, hasDeprecationWarnings}) => {
const _run = async (
configs: Array<{config: Config, hasDeprecationWarnings: boolean}>,
) => {
if (argv.debug) {
logDebugMessages(config, pipe);
// TODO fix/remove this: there should be a `--show-config` argument.
logDebugMessages(configs[0].config, pipe);
}

createDirectory(config.cacheDirectory);
const hasteMapInstance = Runtime.createHasteMap(config, {
console: new Console(pipe, pipe),
maxWorkers: getMaxWorkers(argv),
resetCache: !config.cache,
watch: config.watch,
});

const hasteMap = await hasteMapInstance.build();
const hasteContext = createHasteContext(config, hasteMap);
if (argv.watch || argv.watchAll) {
const {config} = configs[0];
createDirectory(config.cacheDirectory);
const hasteMapInstance = Runtime.createHasteMap(config, {
console: new Console(pipe, pipe),
maxWorkers: getMaxWorkers(argv),
resetCache: !config.cache,
watch: config.watch,
});

const hasteMap = await hasteMapInstance.build();
const hasteContext = createTestContext(config, hasteMap);
return watch(
config,
pipe,
argv,
hasteMapInstance,
hasteContext,
hasDeprecationWarnings,
// TODO
configs[0].hasDeprecationWarnings,
);
} else {
const contexts = await Promise.all(
configs.map(async ({config}) => {
createDirectory(config.cacheDirectory);
return createTestContext(
config,
await Runtime.createHasteMap(config, {
console: new Console(pipe, pipe),
maxWorkers: getMaxWorkers(argv),
resetCache: !config.cache,
watch: config.watch,
}).build(),
);
}),
);

const startRun = () => {
preRunMessage.print(pipe);
const testWatcher = new TestWatcher({isWatchMode: false});
return runJest(
hasteContext,
config,
argv,
pipe,
testWatcher,
startRun,
onComplete,
);
runJest(contexts, argv, pipe, testWatcher, startRun, onComplete);
};
return startRun();
}
};

readConfig(argv, root).then(_run).catch(error => {
try {
await _run(await Promise.all(roots.map(root => readConfig(argv, root))));
} catch (error) {
clearLine(process.stderr);
clearLine(process.stdout);
console.error(chalk.red(error.stack));
process.exit(1);
});
}
};
Loading

0 comments on commit 1a30f7c

Please sign in to comment.