Suite.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = Suite;
  6. var _jestUtil = require('jest-util');
  7. var _expectation_failed = require('../expectation_failed');
  8. var _expectation_failed2 = _interopRequireDefault(_expectation_failed);
  9. var _expectation_result_factory = require('../expectation_result_factory');
  10. var _expectation_result_factory2 = _interopRequireDefault(
  11. _expectation_result_factory
  12. );
  13. function _interopRequireDefault(obj) {
  14. return obj && obj.__esModule ? obj : {default: obj};
  15. }
  16. function Suite(attrs) {
  17. this.id = attrs.id;
  18. this.parentSuite = attrs.parentSuite;
  19. this.description = (0, _jestUtil.convertDescriptorToString)(
  20. attrs.description
  21. );
  22. this.throwOnExpectationFailure = !!attrs.throwOnExpectationFailure;
  23. this.beforeFns = [];
  24. this.afterFns = [];
  25. this.beforeAllFns = [];
  26. this.afterAllFns = [];
  27. this.disabled = false;
  28. this.children = [];
  29. this.result = {
  30. id: this.id,
  31. description: this.description,
  32. fullName: this.getFullName(),
  33. failedExpectations: [],
  34. testPath: attrs.getTestPath()
  35. };
  36. }
  37. /**
  38. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  39. *
  40. * This source code is licensed under the MIT license found in the
  41. * LICENSE file in the root directory of this source tree.
  42. *
  43. */
  44. // This file is a heavily modified fork of Jasmine. Original license:
  45. /*
  46. Copyright (c) 2008-2016 Pivotal Labs
  47. Permission is hereby granted, free of charge, to any person obtaining
  48. a copy of this software and associated documentation files (the
  49. "Software"), to deal in the Software without restriction, including
  50. without limitation the rights to use, copy, modify, merge, publish,
  51. distribute, sublicense, and/or sell copies of the Software, and to
  52. permit persons to whom the Software is furnished to do so, subject to
  53. the following conditions:
  54. The above copyright notice and this permission notice shall be
  55. included in all copies or substantial portions of the Software.
  56. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  57. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  58. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  59. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  60. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  61. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  62. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  63. */
  64. /* eslint-disable sort-keys */
  65. Suite.prototype.getFullName = function() {
  66. const fullName = [];
  67. for (
  68. let parentSuite = this;
  69. parentSuite;
  70. parentSuite = parentSuite.parentSuite
  71. ) {
  72. if (parentSuite.parentSuite) {
  73. fullName.unshift(parentSuite.description);
  74. }
  75. }
  76. return fullName.join(' ');
  77. };
  78. Suite.prototype.disable = function() {
  79. this.disabled = true;
  80. };
  81. Suite.prototype.pend = function(message) {
  82. this.markedPending = true;
  83. };
  84. Suite.prototype.beforeEach = function(fn) {
  85. this.beforeFns.unshift(fn);
  86. };
  87. Suite.prototype.beforeAll = function(fn) {
  88. this.beforeAllFns.push(fn);
  89. };
  90. Suite.prototype.afterEach = function(fn) {
  91. this.afterFns.unshift(fn);
  92. };
  93. Suite.prototype.afterAll = function(fn) {
  94. this.afterAllFns.unshift(fn);
  95. };
  96. Suite.prototype.addChild = function(child) {
  97. this.children.push(child);
  98. };
  99. Suite.prototype.status = function() {
  100. if (this.disabled) {
  101. return 'disabled';
  102. }
  103. if (this.markedPending) {
  104. return 'pending';
  105. }
  106. if (this.result.failedExpectations.length > 0) {
  107. return 'failed';
  108. } else {
  109. return 'finished';
  110. }
  111. };
  112. Suite.prototype.isExecutable = function() {
  113. return !this.disabled;
  114. };
  115. Suite.prototype.canBeReentered = function() {
  116. return this.beforeAllFns.length === 0 && this.afterAllFns.length === 0;
  117. };
  118. Suite.prototype.getResult = function() {
  119. this.result.status = this.status();
  120. return this.result;
  121. };
  122. Suite.prototype.sharedUserContext = function() {
  123. if (!this.sharedContext) {
  124. this.sharedContext = {};
  125. }
  126. return this.sharedContext;
  127. };
  128. Suite.prototype.clonedSharedUserContext = function() {
  129. return this.sharedUserContext();
  130. };
  131. Suite.prototype.onException = function() {
  132. if (arguments[0] instanceof _expectation_failed2.default) {
  133. return;
  134. }
  135. if (isAfterAll(this.children)) {
  136. const data = {
  137. matcherName: '',
  138. passed: false,
  139. expected: '',
  140. actual: '',
  141. error: arguments[0]
  142. };
  143. this.result.failedExpectations.push(
  144. (0, _expectation_result_factory2.default)(data)
  145. );
  146. } else {
  147. for (let i = 0; i < this.children.length; i++) {
  148. const child = this.children[i];
  149. child.onException.apply(child, arguments);
  150. }
  151. }
  152. };
  153. Suite.prototype.addExpectationResult = function() {
  154. if (isAfterAll(this.children) && isFailure(arguments)) {
  155. const data = arguments[1];
  156. this.result.failedExpectations.push(
  157. (0, _expectation_result_factory2.default)(data)
  158. );
  159. if (this.throwOnExpectationFailure) {
  160. throw new _expectation_failed2.default();
  161. }
  162. } else {
  163. for (let i = 0; i < this.children.length; i++) {
  164. const child = this.children[i];
  165. try {
  166. child.addExpectationResult.apply(child, arguments);
  167. } catch (e) {
  168. // keep going
  169. }
  170. }
  171. }
  172. };
  173. function isAfterAll(children) {
  174. return children && children[0] && children[0].result.status;
  175. }
  176. function isFailure(args) {
  177. return !args[0];
  178. }