aboutsummaryrefslogtreecommitdiff
path: root/node_modules/@jest/core/build/plugins/UpdateSnapshotsInteractive.js
blob: 5346bc35357b42bd9a362410e0f8ea864fac262c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
'use strict';

Object.defineProperty(exports, '__esModule', {
  value: true
});
exports.default = void 0;

function _jestWatcher() {
  const data = require('jest-watcher');

  _jestWatcher = function () {
    return data;
  };

  return data;
}

var _SnapshotInteractiveMode = _interopRequireDefault(
  require('../SnapshotInteractiveMode')
);

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;
}

class UpdateSnapshotInteractivePlugin extends _jestWatcher().BaseWatchPlugin {
  constructor(...args) {
    super(...args);

    _defineProperty(
      this,
      '_snapshotInteractiveMode',
      new _SnapshotInteractiveMode.default(this._stdout)
    );

    _defineProperty(this, '_failedSnapshotTestAssertions', []);

    _defineProperty(this, 'isInternal', true);
  }

  getFailedSnapshotTestAssertions(testResults) {
    const failedTestPaths = [];

    if (testResults.numFailedTests === 0 || !testResults.testResults) {
      return failedTestPaths;
    }

    testResults.testResults.forEach(testResult => {
      if (testResult.snapshot && testResult.snapshot.unmatched) {
        testResult.testResults.forEach(result => {
          if (result.status === 'failed') {
            failedTestPaths.push({
              fullName: result.fullName,
              path: testResult.testFilePath
            });
          }
        });
      }
    });
    return failedTestPaths;
  }

  apply(hooks) {
    hooks.onTestRunComplete(results => {
      this._failedSnapshotTestAssertions =
        this.getFailedSnapshotTestAssertions(results);

      if (this._snapshotInteractiveMode.isActive()) {
        this._snapshotInteractiveMode.updateWithResults(results);
      }
    });
  }

  onKey(key) {
    if (this._snapshotInteractiveMode.isActive()) {
      this._snapshotInteractiveMode.put(key);
    }
  }

  run(_globalConfig, updateConfigAndRun) {
    if (this._failedSnapshotTestAssertions.length) {
      return new Promise(res => {
        this._snapshotInteractiveMode.run(
          this._failedSnapshotTestAssertions,
          (assertion, shouldUpdateSnapshot) => {
            updateConfigAndRun({
              mode: 'watch',
              testNamePattern: assertion ? `^${assertion.fullName}$` : '',
              testPathPattern: assertion ? assertion.path : '',
              updateSnapshot: shouldUpdateSnapshot ? 'all' : 'none'
            });

            if (!this._snapshotInteractiveMode.isActive()) {
              res();
            }
          }
        );
      });
    } else {
      return Promise.resolve();
    }
  }

  getUsageInfo() {
    var _this$_failedSnapshot;

    if (
      ((_this$_failedSnapshot = this._failedSnapshotTestAssertions) === null ||
      _this$_failedSnapshot === void 0
        ? void 0
        : _this$_failedSnapshot.length) > 0
    ) {
      return {
        key: 'i',
        prompt: 'update failing snapshots interactively'
      };
    }

    return null;
  }
}

var _default = UpdateSnapshotInteractivePlugin;
exports.default = _default;