Skip to content

Commit 35ac3cc

Browse files
cpojerFacebook Github Bot 0
authored andcommitted
Clear console when using --watch.
Summary:It also changes the behavior of watch to only run changed tests (by turning on the `onlyChanged` flag). All tests can still be run by doing `jest --watch=all`. Closes jestjs#831 Reviewed By: vjeux Differential Revision: D3090183 fb-gh-sync-id: 2dbfeba9058ecaa3cbb2fe4be67dd532933459ee shipit-source-id: 2dbfeba9058ecaa3cbb2fe4be67dd532933459ee
1 parent aaae25b commit 35ac3cc

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
* Added `jest-util`, general code cleanup.
1010
* Fixed an error with Jasmine 2 and tests that `throw 'string errors'`.
1111
* Fixed issues with unmocking symlinked module names.
12+
* Clear the terminal window when using `--watch`.
13+
* By default, `--watch` will now only runs tests related to changed files.
14+
`--watch=all` can be used to run all tests on file system changes.
1215

1316
## jest-cli 0.9.2, babel-jest 9.0.3
1417

src/cli/processArgs.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,9 @@ function processArgs() {
122122
},
123123
watch: {
124124
description: _wrapDesc(
125-
'Watch files for changes and rerun tests related to changed files ' +
126-
'and directories. Works with `--onlyChanged` to only run the ' +
127-
'affected tests.'
128-
),
129-
type: 'boolean',
130-
},
131-
watchExtensions: {
132-
description: _wrapDesc(
133-
'Comma separated list of file extensions to watch, defaults to js.'
125+
'Watch files for changes and rerun tests related to changed files. ' +
126+
'If you want to re-run all tests when a file has changed, you can ' +
127+
'call Jest using `--watch=all`.'
134128
),
135129
type: 'string',
136130
},
@@ -188,7 +182,7 @@ function processArgs() {
188182
type: 'boolean',
189183
},
190184
})
191-
.check(function(argv) {
185+
.check(argv => {
192186
if (argv.runInBand && argv.hasOwnProperty('maxWorkers')) {
193187
throw new Error(
194188
'Both --runInBand and --maxWorkers were specified, but these two ' +

src/jest.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const readConfig = require('./config/read');
2525
const sane = require('sane');
2626
const which = require('which');
2727

28-
const DEFAULT_WATCH_EXTENSIONS = 'js';
28+
const CLEAR = '\x1B[2J\x1B[H';
2929
const WATCHER_DEBOUNCE = 200;
3030
const WATCHMAN_BIN = 'watchman';
3131

@@ -114,11 +114,12 @@ function getNoTestsFoundMessage(patternInfo) {
114114
`${message} Regex used while searching: ${formattedPattern}.`;
115115
}
116116

117-
function getWatcher(argv, packageRoot, callback) {
117+
function getWatcher(config, packageRoot, callback) {
118118
which(WATCHMAN_BIN, (err, resolvedPath) => {
119119
const watchman = !err && resolvedPath;
120-
const extensions = argv.watchExtensions || DEFAULT_WATCH_EXTENSIONS;
121-
const glob = extensions.split(',').map(extension => '**/*' + extension);
120+
const glob = config.moduleFileExtensions
121+
.concat(config.testFileExtensions)
122+
.map(extension => '**/*' + extension);
122123
const watcher = sane(packageRoot, {glob, watchman});
123124
callback(watcher);
124125
});
@@ -186,13 +187,19 @@ function runCLI(argv, root, onComplete) {
186187
}
187188

188189
const prefix = argv.watch ? 'Watch using' : 'Using';
189-
pipe.write(`${prefix} Jest CLI ${info.join(', ')}\n`);
190-
if (argv.watch) {
191-
getWatcher(argv, root, watcher => {
190+
const header = `${prefix} Jest CLI ${info.join(', ')}\n`;
191+
if (argv.watch !== undefined) {
192+
if (argv.watch !== 'all') {
193+
argv.onlyChanged = true;
194+
}
195+
196+
getWatcher(config, root, watcher => {
192197
let timer;
193198
let isRunning;
194199

200+
pipe.write(CLEAR + header);
195201
watcher.on('all', (_, filePath) => {
202+
pipe.write(CLEAR + header);
196203
filePath = path.join(root, filePath);
197204
const isValidPath =
198205
config.testPathDirs.some(dir => filePath.startsWith(dir));
@@ -212,6 +219,7 @@ function runCLI(argv, root, onComplete) {
212219
});
213220
});
214221
} else {
222+
pipe.write(header);
215223
return runJest(config, argv, pipe, onComplete);
216224
}
217225
}, error => {

0 commit comments

Comments
 (0)