UpdateSnapshotsInteractive.js 3.3 KB

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