Skip to content

Commit

Permalink
[1] Add --watchAll option and run tests immediately on start up (#1475)
Browse files Browse the repository at this point in the history
* Replace --watch=all option with --watchAll

* Run tests immediately on start up when using --watch or --watchAll
  • Loading branch information
xadn authored and cpojer committed Aug 29, 2016
1 parent 8b0ced0 commit 2031985
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/jest-cli/src/SearchSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class SearchSource {
): string {
if (patternInfo.onlyChanged) {
const guide = patternInfo.watch
? 'starting Jest with `jest --watch=all`'
? 'starting Jest with `jest --watchAll`'
: 'running Jest without `-o`';
return 'No tests found related to changed and uncommitted files.\n' +
'Note: If you are using dynamic `require`-calls or no tests related ' +
Expand Down
20 changes: 15 additions & 5 deletions packages/jest-cli/src/cli/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ const check = (argv: Object) => {
);
}

if (argv.watchExtensions && argv.watch === undefined) {
if (argv.onlyChanged && argv.watchAll) {
throw new Error(
'--watchExtensions can only be specified together with --watch.',
'Both --onlyChanged and --watchAll were specified, but these two ' +
'options do not make sense together. Try the --watch option which ' +
'reruns only tests related to changed files.',
);
}

Expand Down Expand Up @@ -148,10 +150,18 @@ const options = {
watch: {
description: wrap(
'Watch files for changes and rerun tests related to changed files. ' +
'If you want to re-run all tests when a file has changed, you can ' +
'call Jest using `--watch=all`.',
'If you want to re-run all tests when a file has changed, use the ' +
'`--watchAll` option.',
),
type: 'string',
type: 'boolean',
},
watchAll: {
description: wrap(
'Watch files for changes and rerun all tests. If you want to re-run ' +
'only the tests related to the changed files, use the ' +
'`--watch` option.',
),
type: 'boolean',
},
bail: {
alias: 'b',
Expand Down
30 changes: 14 additions & 16 deletions packages/jest-cli/src/jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function buildTestPathPatternInfo(argv) {
return {
lastCommit: argv.lastCommit,
onlyChanged: true,
watch: argv.watch !== undefined,
watch: argv.watch,
};
}
if (argv.testPathPattern) {
Expand Down Expand Up @@ -151,17 +151,25 @@ function runCLI(argv: Object, root: Path, onComplete: () => void) {
pipe.write('test framework = ' + testFramework.name + '\n');
pipe.write('config = ' + JSON.stringify(config, null, ' ') + '\n');
}
if (argv.watch !== undefined) {
if (argv.watch !== 'all') {
argv.onlyChanged = true;
}
if (argv.watch || argv.watchAll) {
argv.onlyChanged = !argv.watchAll;

return new Promise(resolve => {
getWatcher(config, root, watcher => {
let timer;
let isRunning;
const startRun = () => {
isRunning = true;
runJest(config, argv, pipe, () => isRunning = false)
.then(
resolve,
error => console.error(chalk.red(error)),
);
};

pipe.write(CLEAR);
startRun();

watcher.on('all', (_, filePath) => {
pipe.write(CLEAR);
filePath = path.join(root, filePath);
Expand All @@ -172,17 +180,7 @@ function runCLI(argv: Object, root: Path, onComplete: () => void) {
clearTimeout(timer);
timer = null;
}
timer = setTimeout(
() => {
isRunning = true;
runJest(config, argv, pipe, () => isRunning = false)
.then(
resolve,
error => console.error(chalk.red(error)),
);
},
WATCHER_DEBOUNCE,
);
timer = setTimeout(startRun, WATCHER_DEBOUNCE);
}
});
});
Expand Down

0 comments on commit 2031985

Please sign in to comment.