123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- 'use strict';
- Object.defineProperty(exports, '__esModule', {
- value: true
- });
- exports.default = void 0;
- function _ansiEscapes() {
- const data = _interopRequireDefault(require('ansi-escapes'));
- _ansiEscapes = function () {
- return data;
- };
- return data;
- }
- function _chalk() {
- const data = _interopRequireDefault(require('chalk'));
- _chalk = function () {
- return data;
- };
- return data;
- }
- function _jestUtil() {
- const data = require('jest-util');
- _jestUtil = function () {
- return data;
- };
- return data;
- }
- function _jestWatcher() {
- const data = require('jest-watcher');
- _jestWatcher = function () {
- return data;
- };
- return data;
- }
- function _interopRequireDefault(obj) {
- return obj && obj.__esModule ? obj : {default: obj};
- }
- function _defineProperty(obj, key, value) {
- if (key in obj) {
- Object.defineProperty(obj, key, {
- value: value,
- enumerable: true,
- configurable: true,
- writable: true
- });
- } else {
- obj[key] = value;
- }
- return obj;
- }
- const {ARROW, CLEAR} = _jestUtil().specialChars;
- class SnapshotInteractiveMode {
- constructor(pipe) {
- _defineProperty(this, '_pipe', void 0);
- _defineProperty(this, '_isActive', void 0);
- _defineProperty(this, '_updateTestRunnerConfig', void 0);
- _defineProperty(this, '_testAssertions', void 0);
- _defineProperty(this, '_countPaths', void 0);
- _defineProperty(this, '_skippedNum', void 0);
- this._pipe = pipe;
- this._isActive = false;
- this._skippedNum = 0;
- }
- isActive() {
- return this._isActive;
- }
- getSkippedNum() {
- return this._skippedNum;
- }
- _clearTestSummary() {
- this._pipe.write(_ansiEscapes().default.cursorUp(6));
- this._pipe.write(_ansiEscapes().default.eraseDown);
- }
- _drawUIProgress() {
- this._clearTestSummary();
- const numPass = this._countPaths - this._testAssertions.length;
- const numRemaining = this._countPaths - numPass - this._skippedNum;
- let stats = _chalk().default.bold.dim(
- (0, _jestUtil().pluralize)('snapshot', numRemaining) + ' remaining'
- );
- if (numPass) {
- stats +=
- ', ' +
- _chalk().default.bold.green(
- (0, _jestUtil().pluralize)('snapshot', numPass) + ' updated'
- );
- }
- if (this._skippedNum) {
- stats +=
- ', ' +
- _chalk().default.bold.yellow(
- (0, _jestUtil().pluralize)('snapshot', this._skippedNum) + ' skipped'
- );
- }
- const messages = [
- '\n' + _chalk().default.bold('Interactive Snapshot Progress'),
- ARROW + stats,
- '\n' + _chalk().default.bold('Watch Usage'),
- _chalk().default.dim(ARROW + 'Press ') +
- 'u' +
- _chalk().default.dim(' to update failing snapshots for this test.'),
- _chalk().default.dim(ARROW + 'Press ') +
- 's' +
- _chalk().default.dim(' to skip the current test.'),
- _chalk().default.dim(ARROW + 'Press ') +
- 'q' +
- _chalk().default.dim(' to quit Interactive Snapshot Mode.'),
- _chalk().default.dim(ARROW + 'Press ') +
- 'Enter' +
- _chalk().default.dim(' to trigger a test run.')
- ];
- this._pipe.write(messages.filter(Boolean).join('\n') + '\n');
- }
- _drawUIDoneWithSkipped() {
- this._pipe.write(CLEAR);
- const numPass = this._countPaths - this._testAssertions.length;
- let stats = _chalk().default.bold.dim(
- (0, _jestUtil().pluralize)('snapshot', this._countPaths) + ' reviewed'
- );
- if (numPass) {
- stats +=
- ', ' +
- _chalk().default.bold.green(
- (0, _jestUtil().pluralize)('snapshot', numPass) + ' updated'
- );
- }
- if (this._skippedNum) {
- stats +=
- ', ' +
- _chalk().default.bold.yellow(
- (0, _jestUtil().pluralize)('snapshot', this._skippedNum) + ' skipped'
- );
- }
- const messages = [
- '\n' + _chalk().default.bold('Interactive Snapshot Result'),
- ARROW + stats,
- '\n' + _chalk().default.bold('Watch Usage'),
- _chalk().default.dim(ARROW + 'Press ') +
- 'r' +
- _chalk().default.dim(' to restart Interactive Snapshot Mode.'),
- _chalk().default.dim(ARROW + 'Press ') +
- 'q' +
- _chalk().default.dim(' to quit Interactive Snapshot Mode.')
- ];
- this._pipe.write(messages.filter(Boolean).join('\n') + '\n');
- }
- _drawUIDone() {
- this._pipe.write(CLEAR);
- const numPass = this._countPaths - this._testAssertions.length;
- let stats = _chalk().default.bold.dim(
- (0, _jestUtil().pluralize)('snapshot', this._countPaths) + ' reviewed'
- );
- if (numPass) {
- stats +=
- ', ' +
- _chalk().default.bold.green(
- (0, _jestUtil().pluralize)('snapshot', numPass) + ' updated'
- );
- }
- const messages = [
- '\n' + _chalk().default.bold('Interactive Snapshot Result'),
- ARROW + stats,
- '\n' + _chalk().default.bold('Watch Usage'),
- _chalk().default.dim(ARROW + 'Press ') +
- 'Enter' +
- _chalk().default.dim(' to return to watch mode.')
- ];
- this._pipe.write(messages.filter(Boolean).join('\n') + '\n');
- }
- _drawUIOverlay() {
- if (this._testAssertions.length === 0) {
- return this._drawUIDone();
- }
- if (this._testAssertions.length - this._skippedNum === 0) {
- return this._drawUIDoneWithSkipped();
- }
- return this._drawUIProgress();
- }
- put(key) {
- switch (key) {
- case 's':
- if (this._skippedNum === this._testAssertions.length) break;
- this._skippedNum += 1; // move skipped test to the end
- this._testAssertions.push(this._testAssertions.shift());
- if (this._testAssertions.length - this._skippedNum > 0) {
- this._run(false);
- } else {
- this._drawUIDoneWithSkipped();
- }
- break;
- case 'u':
- this._run(true);
- break;
- case 'q':
- case _jestWatcher().KEYS.ESCAPE:
- this.abort();
- break;
- case 'r':
- this.restart();
- break;
- case _jestWatcher().KEYS.ENTER:
- if (this._testAssertions.length === 0) {
- this.abort();
- } else {
- this._run(false);
- }
- break;
- default:
- break;
- }
- }
- abort() {
- this._isActive = false;
- this._skippedNum = 0;
- this._updateTestRunnerConfig(null, false);
- }
- restart() {
- this._skippedNum = 0;
- this._countPaths = this._testAssertions.length;
- this._run(false);
- }
- updateWithResults(results) {
- const hasSnapshotFailure = !!results.snapshot.failure;
- if (hasSnapshotFailure) {
- this._drawUIOverlay();
- return;
- }
- this._testAssertions.shift();
- if (this._testAssertions.length - this._skippedNum === 0) {
- this._drawUIOverlay();
- return;
- } // Go to the next test
- this._run(false);
- }
- _run(shouldUpdateSnapshot) {
- const testAssertion = this._testAssertions[0];
- this._updateTestRunnerConfig(testAssertion, shouldUpdateSnapshot);
- }
- run(failedSnapshotTestAssertions, onConfigChange) {
- if (!failedSnapshotTestAssertions.length) {
- return;
- }
- this._testAssertions = [...failedSnapshotTestAssertions];
- this._countPaths = this._testAssertions.length;
- this._updateTestRunnerConfig = onConfigChange;
- this._isActive = true;
- this._run(false);
- }
- }
- exports.default = SnapshotInteractiveMode;
|