Suite.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _jestUtil = require('jest-util');
  7. var _ExpectationFailed = _interopRequireDefault(
  8. require('../ExpectationFailed')
  9. );
  10. var _expectationResultFactory = _interopRequireDefault(
  11. require('../expectationResultFactory')
  12. );
  13. function _interopRequireDefault(obj) {
  14. return obj && obj.__esModule ? obj : {default: obj};
  15. }
  16. function _defineProperty(obj, key, value) {
  17. if (key in obj) {
  18. Object.defineProperty(obj, key, {
  19. value: value,
  20. enumerable: true,
  21. configurable: true,
  22. writable: true
  23. });
  24. } else {
  25. obj[key] = value;
  26. }
  27. return obj;
  28. }
  29. class Suite {
  30. constructor(attrs) {
  31. _defineProperty(this, 'id', void 0);
  32. _defineProperty(this, 'parentSuite', void 0);
  33. _defineProperty(this, 'description', void 0);
  34. _defineProperty(this, 'throwOnExpectationFailure', void 0);
  35. _defineProperty(this, 'beforeFns', void 0);
  36. _defineProperty(this, 'afterFns', void 0);
  37. _defineProperty(this, 'beforeAllFns', void 0);
  38. _defineProperty(this, 'afterAllFns', void 0);
  39. _defineProperty(this, 'disabled', void 0);
  40. _defineProperty(this, 'children', void 0);
  41. _defineProperty(this, 'result', void 0);
  42. _defineProperty(this, 'sharedContext', void 0);
  43. _defineProperty(this, 'markedPending', void 0);
  44. _defineProperty(this, 'markedTodo', void 0);
  45. _defineProperty(this, 'isFocused', void 0);
  46. this.markedPending = false;
  47. this.markedTodo = false;
  48. this.isFocused = false;
  49. this.id = attrs.id;
  50. this.parentSuite = attrs.parentSuite;
  51. this.description = (0, _jestUtil.convertDescriptorToString)(
  52. attrs.description
  53. );
  54. this.throwOnExpectationFailure = !!attrs.throwOnExpectationFailure;
  55. this.beforeFns = [];
  56. this.afterFns = [];
  57. this.beforeAllFns = [];
  58. this.afterAllFns = [];
  59. this.disabled = false;
  60. this.children = [];
  61. this.result = {
  62. id: this.id,
  63. description: this.description,
  64. fullName: this.getFullName(),
  65. failedExpectations: [],
  66. testPath: attrs.getTestPath()
  67. };
  68. }
  69. getFullName() {
  70. const fullName = [];
  71. for (
  72. let parentSuite = this;
  73. parentSuite;
  74. parentSuite = parentSuite.parentSuite
  75. ) {
  76. if (parentSuite.parentSuite) {
  77. fullName.unshift(parentSuite.description);
  78. }
  79. }
  80. return fullName.join(' ');
  81. }
  82. disable() {
  83. this.disabled = true;
  84. }
  85. pend(_message) {
  86. this.markedPending = true;
  87. }
  88. beforeEach(fn) {
  89. this.beforeFns.unshift(fn);
  90. }
  91. beforeAll(fn) {
  92. this.beforeAllFns.push(fn);
  93. }
  94. afterEach(fn) {
  95. this.afterFns.unshift(fn);
  96. }
  97. afterAll(fn) {
  98. this.afterAllFns.unshift(fn);
  99. }
  100. addChild(child) {
  101. this.children.push(child);
  102. }
  103. status() {
  104. if (this.disabled) {
  105. return 'disabled';
  106. }
  107. if (this.markedPending) {
  108. return 'pending';
  109. }
  110. if (this.result.failedExpectations.length > 0) {
  111. return 'failed';
  112. } else {
  113. return 'finished';
  114. }
  115. }
  116. isExecutable() {
  117. return !this.disabled;
  118. }
  119. canBeReentered() {
  120. return this.beforeAllFns.length === 0 && this.afterAllFns.length === 0;
  121. }
  122. getResult() {
  123. this.result.status = this.status();
  124. return this.result;
  125. }
  126. sharedUserContext() {
  127. if (!this.sharedContext) {
  128. this.sharedContext = {};
  129. }
  130. return this.sharedContext;
  131. }
  132. clonedSharedUserContext() {
  133. return this.sharedUserContext();
  134. }
  135. onException(...args) {
  136. if (args[0] instanceof _ExpectationFailed.default) {
  137. return;
  138. }
  139. if (isAfterAll(this.children)) {
  140. const data = {
  141. matcherName: '',
  142. passed: false,
  143. expected: '',
  144. actual: '',
  145. error: arguments[0]
  146. };
  147. this.result.failedExpectations.push(
  148. (0, _expectationResultFactory.default)(data)
  149. );
  150. } else {
  151. for (let i = 0; i < this.children.length; i++) {
  152. const child = this.children[i];
  153. child.onException.apply(child, args);
  154. }
  155. }
  156. }
  157. addExpectationResult(...args) {
  158. if (isAfterAll(this.children) && isFailure(args)) {
  159. const data = args[1];
  160. this.result.failedExpectations.push(
  161. (0, _expectationResultFactory.default)(data)
  162. );
  163. if (this.throwOnExpectationFailure) {
  164. throw new _ExpectationFailed.default();
  165. }
  166. } else {
  167. for (let i = 0; i < this.children.length; i++) {
  168. const child = this.children[i];
  169. try {
  170. child.addExpectationResult.apply(child, args);
  171. } catch (e) {
  172. // keep going
  173. }
  174. }
  175. }
  176. }
  177. execute(..._args) {}
  178. }
  179. exports.default = Suite;
  180. function isAfterAll(children) {
  181. return children && children[0] && children[0].result.status;
  182. }
  183. function isFailure(args) {
  184. return !args[0];
  185. }