UNPKG

3.12 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.default = void 0;
7
8function _jestWatcher() {
9 const data = require('jest-watcher');
10
11 _jestWatcher = function () {
12 return data;
13 };
14
15 return data;
16}
17
18function _FailedTestsInteractiveMode() {
19 const data = _interopRequireDefault(require('../FailedTestsInteractiveMode'));
20
21 _FailedTestsInteractiveMode = function () {
22 return data;
23 };
24
25 return data;
26}
27
28function _interopRequireDefault(obj) {
29 return obj && obj.__esModule ? obj : {default: obj};
30}
31
32function _defineProperty(obj, key, value) {
33 if (key in obj) {
34 Object.defineProperty(obj, key, {
35 value: value,
36 enumerable: true,
37 configurable: true,
38 writable: true
39 });
40 } else {
41 obj[key] = value;
42 }
43 return obj;
44}
45
46class FailedTestsInteractivePlugin extends _jestWatcher().BaseWatchPlugin {
47 constructor(...args) {
48 super(...args);
49
50 _defineProperty(this, '_failedTestAssertions', void 0);
51
52 _defineProperty(
53 this,
54 '_manager',
55 new (_FailedTestsInteractiveMode().default)(this._stdout)
56 );
57 }
58
59 apply(hooks) {
60 hooks.onTestRunComplete(results => {
61 this._failedTestAssertions = this.getFailedTestAssertions(results);
62 if (this._manager.isActive()) this._manager.updateWithResults(results);
63 });
64 }
65
66 getUsageInfo() {
67 var _this$_failedTestAsse;
68
69 if (
70 (_this$_failedTestAsse = this._failedTestAssertions) !== null &&
71 _this$_failedTestAsse !== void 0 &&
72 _this$_failedTestAsse.length
73 ) {
74 return {
75 key: 'i',
76 prompt: 'run failing tests interactively'
77 };
78 }
79
80 return null;
81 }
82
83 onKey(key) {
84 if (this._manager.isActive()) {
85 this._manager.put(key);
86 }
87 }
88
89 run(_, updateConfigAndRun) {
90 return new Promise(resolve => {
91 if (
92 !this._failedTestAssertions ||
93 this._failedTestAssertions.length === 0
94 ) {
95 resolve();
96 return;
97 }
98
99 this._manager.run(this._failedTestAssertions, failure => {
100 updateConfigAndRun({
101 mode: 'watch',
102 testNamePattern: failure ? `^${failure.fullName}$` : '',
103 testPathPattern:
104 (failure === null || failure === void 0 ? void 0 : failure.path) ||
105 ''
106 });
107
108 if (!this._manager.isActive()) {
109 resolve();
110 }
111 });
112 });
113 }
114
115 getFailedTestAssertions(results) {
116 const failedTestPaths = [];
117
118 if (
119 // skip if no failed tests
120 results.numFailedTests === 0 || // skip if missing test results
121 !results.testResults || // skip if unmatched snapshots are present
122 results.snapshot.unmatched
123 ) {
124 return failedTestPaths;
125 }
126
127 results.testResults.forEach(testResult => {
128 testResult.testResults.forEach(result => {
129 if (result.status === 'failed') {
130 failedTestPaths.push({
131 fullName: result.fullName,
132 path: testResult.testFilePath
133 });
134 }
135 });
136 });
137 return failedTestPaths;
138 }
139}
140
141exports.default = FailedTestsInteractivePlugin;