Spec.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = Spec;
  6. var _assert = require('assert');
  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. var _assert_support = require('../assert_support');
  14. var _assert_support2 = _interopRequireDefault(_assert_support);
  15. function _interopRequireDefault(obj) {
  16. return obj && obj.__esModule ? obj : {default: obj};
  17. }
  18. /**
  19. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  20. *
  21. * This source code is licensed under the MIT license found in the
  22. * LICENSE file in the root directory of this source tree.
  23. *
  24. */
  25. // This file is a heavily modified fork of Jasmine. Original license:
  26. /*
  27. Copyright (c) 2008-2016 Pivotal Labs
  28. Permission is hereby granted, free of charge, to any person obtaining
  29. a copy of this software and associated documentation files (the
  30. "Software"), to deal in the Software without restriction, including
  31. without limitation the rights to use, copy, modify, merge, publish,
  32. distribute, sublicense, and/or sell copies of the Software, and to
  33. permit persons to whom the Software is furnished to do so, subject to
  34. the following conditions:
  35. The above copyright notice and this permission notice shall be
  36. included in all copies or substantial portions of the Software.
  37. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  38. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  39. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  40. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  41. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  42. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  43. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  44. */
  45. /* eslint-disable sort-keys */
  46. function Spec(attrs) {
  47. this.resultCallback = attrs.resultCallback || function() {};
  48. this.id = attrs.id;
  49. this.description = attrs.description || '';
  50. this.queueableFn = attrs.queueableFn;
  51. this.beforeAndAfterFns =
  52. attrs.beforeAndAfterFns ||
  53. function() {
  54. return {befores: [], afters: []};
  55. };
  56. this.userContext =
  57. attrs.userContext ||
  58. function() {
  59. return {};
  60. };
  61. this.onStart = attrs.onStart || function() {};
  62. this.getSpecName =
  63. attrs.getSpecName ||
  64. function() {
  65. return '';
  66. };
  67. this.queueRunnerFactory = attrs.queueRunnerFactory || function() {};
  68. this.throwOnExpectationFailure = !!attrs.throwOnExpectationFailure;
  69. this.initError = new Error();
  70. this.initError.name = '';
  71. this.queueableFn.initError = this.initError;
  72. this.result = {
  73. id: this.id,
  74. description: this.description,
  75. fullName: this.getFullName(),
  76. failedExpectations: [],
  77. passedExpectations: [],
  78. pendingReason: '',
  79. testPath: attrs.getTestPath()
  80. };
  81. }
  82. Spec.prototype.addExpectationResult = function(passed, data, isError) {
  83. const expectationResult = (0, _expectation_result_factory2.default)(
  84. data,
  85. this.initError
  86. );
  87. if (passed) {
  88. this.result.passedExpectations.push(expectationResult);
  89. } else {
  90. this.result.failedExpectations.push(expectationResult);
  91. if (this.throwOnExpectationFailure && !isError) {
  92. throw new _expectation_failed2.default();
  93. }
  94. }
  95. };
  96. Spec.prototype.execute = function(onComplete, enabled) {
  97. const self = this;
  98. this.onStart(this);
  99. if (!this.isExecutable() || this.markedPending || enabled === false) {
  100. complete(enabled);
  101. return;
  102. }
  103. const fns = this.beforeAndAfterFns();
  104. const allFns = fns.befores.concat(this.queueableFn).concat(fns.afters);
  105. this.currentRun = this.queueRunnerFactory({
  106. queueableFns: allFns,
  107. onException: function() {
  108. self.onException.apply(self, arguments);
  109. },
  110. userContext: this.userContext()
  111. });
  112. this.currentRun.then(() => complete(true));
  113. function complete(enabledAgain) {
  114. self.result.status = self.status(enabledAgain);
  115. self.resultCallback(self.result);
  116. if (onComplete) {
  117. onComplete();
  118. }
  119. }
  120. };
  121. Spec.prototype.cancel = function cancel() {
  122. if (this.currentRun) {
  123. this.currentRun.cancel();
  124. }
  125. };
  126. Spec.prototype.onException = function onException(error) {
  127. if (Spec.isPendingSpecException(error)) {
  128. this.pend(extractCustomPendingMessage(error));
  129. return;
  130. }
  131. if (error instanceof _expectation_failed2.default) {
  132. return;
  133. }
  134. if (error instanceof _assert.AssertionError) {
  135. error = (0, _assert_support2.default)(error, {expand: this.expand});
  136. }
  137. this.addExpectationResult(
  138. false,
  139. {
  140. matcherName: '',
  141. passed: false,
  142. expected: '',
  143. actual: '',
  144. error: error
  145. },
  146. true
  147. );
  148. };
  149. Spec.prototype.disable = function() {
  150. this.disabled = true;
  151. };
  152. Spec.prototype.pend = function(message) {
  153. this.markedPending = true;
  154. if (message) {
  155. this.result.pendingReason = message;
  156. }
  157. };
  158. Spec.prototype.getResult = function() {
  159. this.result.status = this.status();
  160. return this.result;
  161. };
  162. Spec.prototype.status = function(enabled) {
  163. if (this.disabled || enabled === false) {
  164. return 'disabled';
  165. }
  166. if (this.markedPending) {
  167. return 'pending';
  168. }
  169. if (this.result.failedExpectations.length > 0) {
  170. return 'failed';
  171. } else {
  172. return 'passed';
  173. }
  174. };
  175. Spec.prototype.isExecutable = function() {
  176. return !this.disabled;
  177. };
  178. Spec.prototype.getFullName = function() {
  179. return this.getSpecName(this);
  180. };
  181. const extractCustomPendingMessage = function(e) {
  182. const fullMessage = e.toString();
  183. const boilerplateStart = fullMessage.indexOf(
  184. Spec.pendingSpecExceptionMessage
  185. );
  186. const boilerplateEnd =
  187. boilerplateStart + Spec.pendingSpecExceptionMessage.length;
  188. return fullMessage.substr(boilerplateEnd);
  189. };
  190. Spec.pendingSpecExceptionMessage = '=> marked Pending';
  191. Spec.isPendingSpecException = function(e) {
  192. return !!(
  193. e &&
  194. e.toString &&
  195. e.toString().indexOf(Spec.pendingSpecExceptionMessage) !== -1
  196. );
  197. };