Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
SnapshotInteractive callback use a bool to mark snapshot update
  • Loading branch information
genintho committed Aug 6, 2017
commit 32127da13957e45af651954a74f8de12ecf54307
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const chalk = require('chalk');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a license not with @flow pragma

import {KEYS} from '../constants';
import SnapshotInteractiveMode from '../SnapshotInteractiveMode';
import SnapshotInteractiveMode from '../snapshot_interactive_mode';

jest.mock('../lib/terminalUtils', () => ({
getTerminalWidth: () => 80,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need Facebook's copyright header. Can you copy it from other files?

* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/

Expand All @@ -7,13 +13,13 @@ import type {AggregatedResult} from 'types/TestResult';
const chalk = require('chalk');
const ansiEscapes = require('ansi-escapes');
const {pluralize} = require('./reporters/utils');
const {rightPad} = require('./lib/terminalUtils');
const {rightPad} = require('./lib/terminal_utils');
const {KEYS} = require('./constants');

module.exports = class SnapshotInteractiveMode {
_pipe: stream$Writable | tty$WriteStream;
_isActive: boolean;
_updateTestRunnerConfig: (a: string, jestRunnerOptions: Object) => *;
_updateTestRunnerConfig: (path: string, shouldUpdateSnapshot: boolean) => *;
_testFilePaths: Array<string>;
_countPaths: number;

Expand All @@ -30,13 +36,13 @@ module.exports = class SnapshotInteractiveMode {
this._pipe.write(ansiEscapes.scrollDown);
this._pipe.write(ansiEscapes.scrollDown);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why do we have to scroll down? Removing these 2 lines leaves the functionality unchanged on iTerm and macOS Terminal.


this._pipe.write(ansiEscapes.cursorSavePosition);
this._pipe.write(ansiEscapes.cursorTo(0, 0));

const title = rightPad(' -> Interactive Snapshot Update Activated <-');
this._pipe.write(chalk.black.bold.bgYellow(title));

this._pipe.write(ansiEscapes.cursorRestorePosition);
// this._pipe.write(ansiEscapes.cursorSavePosition);
// this._pipe.write(ansiEscapes.cursorTo(0, 0));
//
// const title = rightPad(' -> Interactive Snapshot Update Activated <-');
// this._pipe.write(chalk.black.bold.bgYellow(title));
//
// this._pipe.write(ansiEscapes.cursorRestorePosition);
this._pipe.write(ansiEscapes.cursorUp(6));
this._pipe.write(ansiEscapes.eraseDown);

Expand All @@ -51,17 +57,21 @@ module.exports = class SnapshotInteractiveMode {
'\n' + chalk.bold('Interactive Snapshot Progress'),
' \u203A ' + stats,
'\n' + chalk.bold('Watch Usage'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to Interactive Snapshot Mode Usage


chalk.dim(' \u203A Press ') +
'u' +
chalk.dim(' to update failing snapshots.'),
chalk.dim(' to update failing snapshots for this test.'),

this._testFilePaths.length > 1
? chalk.dim(' \u203A Press ') +
's' +
chalk.dim(' to skip the current snapshot..')
's' +
chalk.dim(' to skip the current snapshot.')
: '',

chalk.dim(' \u203A Press ') +
'q' +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to Esc, like we do in Pattern Mode

chalk.dim(' to quit interactive snapshot mode.'),
chalk.dim(' to quit Interactive Snapshot Update Mode.'),

chalk.dim(' \u203A Press ') +
'Enter' +
chalk.dim(' to trigger a test run.'),
Expand All @@ -75,30 +85,26 @@ module.exports = class SnapshotInteractiveMode {
case KEYS.S:
const testFilePath = this._testFilePaths.shift();
this._testFilePaths.push(testFilePath);
this._run({});
this._run(false);
break;

case KEYS.U:
this._run({updateSnapshot: 'all'});
this._run(true);
break;

case KEYS.Q:
case KEYS.ESCAPE:
this.abort();
break;

case KEYS.ENTER:
this._run({});
this._run(false);
break;
default:
console.log('got key event', key);
break;
}
}

abort() {
this._isActive = false;
this._updateTestRunnerConfig('', {});
this._updateTestRunnerConfig('', false);
}

updateWithResults(results: AggregatedResult) {
Expand All @@ -113,17 +119,17 @@ module.exports = class SnapshotInteractiveMode {
this.abort();
return;
}
this._run({});
this._run(false);
}

_run(jestRunnerOptions: Object) {
_run(shouldUpdateSnapshot: boolean) {
const testFilePath = this._testFilePaths[0];
this._updateTestRunnerConfig(testFilePath, jestRunnerOptions);
this._updateTestRunnerConfig(testFilePath, shouldUpdateSnapshot);
}

run(
failedSnapshotTestPaths: Array<string>,
onConfigChange: (path: string, jestRunnerOptions: Object) => *,
onConfigChange: (path: string, shouldUpdateSnapshot: boolean) => *,
) {
if (!failedSnapshotTestPaths.length) {
return;
Expand All @@ -133,6 +139,6 @@ module.exports = class SnapshotInteractiveMode {
this._countPaths = this._testFilePaths.length;
this._updateTestRunnerConfig = onConfigChange;
this._isActive = true;
this._run({});
this._run(false);
}
};
26 changes: 19 additions & 7 deletions packages/jest-cli/src/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const watch = (
const prompt = new Prompt();
const testPathPatternPrompt = new TestPathPatternPrompt(outputStream, prompt);
const testNamePatternPrompt = new TestNamePatternPrompt(outputStream, prompt);
const snapshotInteracticeMode = new SnapshotInteractiveMode(pipe);
const snapshotInteracticeMode = new SnapshotInteractiveMode(outputStream);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: snapshotInteractiveMode

let failedSnapshotTestPaths = [];
let searchSources = contexts.map(context => ({
context,
Expand Down Expand Up @@ -197,8 +197,19 @@ const watch = (
if (hasSnapshotFailure) {
snapshotInteracticeMode.run(
failedSnapshotTestPaths,
(path: string, jestRunnerOptions: Object) => {
updateRunnerPatternMatching('watch', '', path, jestRunnerOptions);
(path: string, shouldUpdateSnapshot: boolean) => {
// updateRunnerPatternMatching('watch', '', path, jestRunnerOptions);
globalConfig = updateGlobalConfig(globalConfig, {
mode: 'watch',
testNamePattern: '',
testPathPattern: replacePathSepForRegex(path),
updateSnapshot: shouldUpdateSnapshot ? 'all' : 'none',
});
startRun(globalConfig);
globalConfig = updateGlobalConfig(globalConfig, {
// updateSnapshot is not sticky after a run.
updateSnapshot: 'none',
});
},
);
}
Expand Down Expand Up @@ -284,11 +295,12 @@ const watch = (
filePattern: string,
jestRunnerOptions = {},
) => {
updateArgv(argv, watchMode, {
globalConfig = updateGlobalConfig(globalConfig, {
mode: watchMode,
testNamePattern: namePattern,
testPathPattern: replacePathSepForRegex(filePattern),
});
startRun(jestRunnerOptions);
startRun(globalConfig);
};

if (typeof stdin.setRawMode === 'function') {
Expand Down Expand Up @@ -354,8 +366,8 @@ const usage = (globalConfig, snapshotFailure, delimiter = '\n') => {

snapshotFailure
? chalk.dim(' \u203A Press ') +
'i' +
chalk.dim(' to update failing snapshots interactively.')
'i' +
chalk.dim(' to update failing snapshots interactively.')
: null,

chalk.dim(' \u203A Press ') +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const getFailedSnapshotTests = require('../getFailedSnapshotTests');
const getFailedSnapshotTests = require('../get_failed_snapshot_tests');

test('return a list of path', () => {
const targetFilename = 'somewhere.js';
Expand Down