UpdateSnapshotsInteractive.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 _SnapshotInteractiveMode = _interopRequireDefault(
  14. require('../SnapshotInteractiveMode')
  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 UpdateSnapshotInteractivePlugin extends _jestWatcher().BaseWatchPlugin {
  33. constructor(...args) {
  34. super(...args);
  35. _defineProperty(
  36. this,
  37. '_snapshotInteractiveMode',
  38. new _SnapshotInteractiveMode.default(this._stdout)
  39. );
  40. _defineProperty(this, '_failedSnapshotTestAssertions', []);
  41. _defineProperty(this, 'isInternal', true);
  42. }
  43. getFailedSnapshotTestAssertions(testResults) {
  44. const failedTestPaths = [];
  45. if (testResults.numFailedTests === 0 || !testResults.testResults) {
  46. return failedTestPaths;
  47. }
  48. testResults.testResults.forEach(testResult => {
  49. if (testResult.snapshot && testResult.snapshot.unmatched) {
  50. testResult.testResults.forEach(result => {
  51. if (result.status === 'failed') {
  52. failedTestPaths.push({
  53. fullName: result.fullName,
  54. path: testResult.testFilePath
  55. });
  56. }
  57. });
  58. }
  59. });
  60. return failedTestPaths;
  61. }
  62. apply(hooks) {
  63. hooks.onTestRunComplete(results => {
  64. this._failedSnapshotTestAssertions =
  65. this.getFailedSnapshotTestAssertions(results);
  66. if (this._snapshotInteractiveMode.isActive()) {
  67. this._snapshotInteractiveMode.updateWithResults(results);
  68. }
  69. });
  70. }
  71. onKey(key) {
  72. if (this._snapshotInteractiveMode.isActive()) {
  73. this._snapshotInteractiveMode.put(key);
  74. }
  75. }
  76. run(_globalConfig, updateConfigAndRun) {
  77. if (this._failedSnapshotTestAssertions.length) {
  78. return new Promise(res => {
  79. this._snapshotInteractiveMode.run(
  80. this._failedSnapshotTestAssertions,
  81. (assertion, shouldUpdateSnapshot) => {
  82. updateConfigAndRun({
  83. mode: 'watch',
  84. testNamePattern: assertion ? `^${assertion.fullName}$` : '',
  85. testPathPattern: assertion ? assertion.path : '',
  86. updateSnapshot: shouldUpdateSnapshot ? 'all' : 'none'
  87. });
  88. if (!this._snapshotInteractiveMode.isActive()) {
  89. res();
  90. }
  91. }
  92. );
  93. });
  94. } else {
  95. return Promise.resolve();
  96. }
  97. }
  98. getUsageInfo() {
  99. var _this$_failedSnapshot;
  100. if (
  101. ((_this$_failedSnapshot = this._failedSnapshotTestAssertions) === null ||
  102. _this$_failedSnapshot === void 0
  103. ? void 0
  104. : _this$_failedSnapshot.length) > 0
  105. ) {
  106. return {
  107. key: 'i',
  108. prompt: 'update failing snapshots interactively'
  109. };
  110. }
  111. return null;
  112. }
  113. }
  114. var _default = UpdateSnapshotInteractivePlugin;
  115. exports.default = _default;