FailedTestsInteractive.js 2.9 KB

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