Skip to content

Commit

Permalink
Update snapshots from within watch mode
Browse files Browse the repository at this point in the history
  • Loading branch information
xadn committed Aug 24, 2016
1 parent 2e734c1 commit 351a46e
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 35 deletions.
98 changes: 68 additions & 30 deletions packages/jest-cli/src/jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ const CLEAR = '\x1B[2J\x1B[H';
const VERSION = require('../package.json').version;
const WATCHER_DEBOUNCE = 200;
const WATCHMAN_BIN = 'watchman';
const KEYS = {
CONTROL_C: '03',
CONTROL_D: '04',
ENTER: '0d',
U: '75',
};

function getMaxWorkers(argv) {
if (argv.runInBand) {
Expand Down Expand Up @@ -154,37 +160,69 @@ function runCLI(argv: Object, root: Path, onComplete: () => void) {
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);
const isValidPath =
config.testPathDirs.some(dir => filePath.startsWith(dir));
if (!isRunning && isValidPath) {
if (timer) {
clearTimeout(timer);
timer = null;
}
timer = setTimeout(startRun, WATCHER_DEBOUNCE);
}
});
});
let isRunning: ?boolean;
let timer: ?int;

const startRun = (overrideConfig: Object = {}) => {
if (isRunning) {
return;
}

pipe.write(CLEAR);
isRunning = true;
runJest(
Object.freeze(Object.assign({}, config, overrideConfig)),
argv,
pipe,
() => {
isRunning = false
},
).then(
() => {},
error => console.error(chalk.red(error)),
);
};

const onKeypress = (key: string) => {
switch (key) {
case KEYS.CONTROL_C:
process.exit(0);
break;
case KEYS.CONTROL_D:
process.exit(0);
break;
case KEYS.ENTER:
startRun();
break;
case KEYS.U:
startRun({updateSnapshot: true});
break;
}
};

const onFileChange = (_, filePath: string) => {
filePath = path.join(root, filePath);
const isValidPath =
config.testPathDirs.some(dir => filePath.startsWith(dir));

if (!isValidPath) {
return;
}
if (timer) {
clearTimeout(timer);
timer = null;
}
timer = setTimeout(startRun, WATCHER_DEBOUNCE);
};

process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.setEncoding('hex');
process.stdin.on('data', onKeypress);
getWatcher(config, root, watcher => {
watcher.on('all', onFileChange);
});
startRun();
} else {
return runJest(config, argv, pipe, onComplete);
}
Expand Down
15 changes: 10 additions & 5 deletions packages/jest-cli/src/reporters/SummaryReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class SummareReporter extends BaseReporter {
}

this._printSummary(aggregatedResults, config);
this._printSnapshotSummary(snapshots);
this._printSnapshotSummary(snapshots, config);
if (totalTestSuites) {
results +=
`${PASS_COLOR(`${pluralize('test', passedTests)} passed`)} (` +
Expand All @@ -73,18 +73,23 @@ class SummareReporter extends BaseReporter {
}
}

_printSnapshotSummary(snapshots: SnapshotSummary) {
_printSnapshotSummary(snapshots: SnapshotSummary, config: Config) {
if (
snapshots.added ||
snapshots.filesRemoved ||
snapshots.unchecked ||
snapshots.unmatched ||
snapshots.updated
) {
let updateCommand;
const event = process.env.npm_lifecycle_event;
const updateCommand =
(!event ? 're-' : '') + 'run with `' +
(event ? 'npm ' + event + ' -- ' : '') + '-u`';
if (config.watch) {
updateCommand = 'press "u"';
} else if (event) {
updateCommand = `run with \`npm ${event} -- -u\``;
} else {
updateCommand = 're-run with `-u`';
}

this.log('\n' + SNAPSHOT_SUMMARY('Snapshot Summary'));
if (snapshots.added) {
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ module.exports = ({
testURL: 'about:blank',
useStderr: false,
verbose: false,
watch: false,
}: DefaultConfig);
5 changes: 5 additions & 0 deletions packages/jest-config/src/setFromArgv.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ function setFromArgv(config, argv) {
if (argv.updateSnapshot) {
config.updateSnapshot = argv.updateSnapshot;
}

if (argv.watch || argv.watchAll) {
config.watch = true;
}

config.noStackTrace = argv.noStackTrace;

config.testcheckOptions = {
Expand Down
1 change: 1 addition & 0 deletions packages/jest-snapshot/src/SnapshotFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class SnapshotFile {

this._content = Object.create(null);
if (this.fileExists(filename)) {
delete require.cache[require.resolve(filename)];
try {
/* eslint-disable no-useless-call */
Object.assign(this._content, require.call(null, filename));
Expand Down
1 change: 1 addition & 0 deletions types/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type BaseConfig = {
testURL: string,
useStderr: boolean,
verbose: boolean,
watch: boolean,
};

export type DefaultConfig = BaseConfig & {
Expand Down

0 comments on commit 351a46e

Please sign in to comment.