Suite.js 5.9 KB

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