kocha
Version:
Modern, simpler Mocha clone, no globals, lint friendly
138 lines (108 loc) • 4.51 kB
JavaScript
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var TestNode = require('./test-node');
var Const = require('./const');
var EVENT = Const.EVENT;
var runCb = TestNode.runCb;
var throwAfterTimeout = TestNode.throwAfterTimeout;
/**
* The runnable node of the test tree.
*
* The parent class of TestCase and TestHook.
*/
var TestRunnableNode = function (_TestNode) {
_inherits(TestRunnableNode, _TestNode);
/**
* @param {string} title The title of the runnable node
* @param {Function} runnable The function which represents the contents of the runnable
* @param {boolean} skipped True iff the runnable node is skipped
* @param {TestSuite} parent The parent suite
*/
function TestRunnableNode(title, runnable, skipped, parent) {
_classCallCheck(this, TestRunnableNode);
var _this = _possibleConstructorReturn(this, (TestRunnableNode.__proto__ || Object.getPrototypeOf(TestRunnableNode)).call(this, title, skipped, parent));
_this.runnable = runnable;
_this.startedAt = 0;
_this.endedAt = 0;
_this.duration = 0;
_this.failCount = 0;
_this.failed = false;
_this.state = null;
return _this;
}
/**
* Returns the timeout duration of the test.
* @return {number}
*/
_createClass(TestRunnableNode, [{
key: 'timeout',
value: function timeout() {
return this.getTimeout();
}
/**
* Fails the node with the given error.
* @param {Error} e The error
*/
}, {
key: 'fail',
value: function fail(e) {
this.failed = true;
this.state = 'failed';
this.bubbleEvent(EVENT.FAIL, this, e);
}
}, {
key: 'start',
value: function start() {
this.startedAt = +new Date();
this.getRunner().setRunningNode(this);
}
}, {
key: 'end',
value: function end() {
this.endedAt = +new Date();
this.duration = this.endedAt - this.startedAt;
this.getRunner().setRunningNode(null);
}
/**
* Returns the threshold number by which the test case is considered slow.
*
* I/F for Reporter
* @return {number}
*/
}, {
key: 'slow',
value: function slow() {
return 75;
}
/**
* Runs the runnable.
*
* Gets the timeout and retry count after starting running the runnable. The user can set them inside test cases or hooks.
*/
}, {
key: 'run',
value: function run() {
var _this2 = this;
if (!this.runnable) {
return;
}
var promise = runCb(this.runnable, function () {
_this2.fail(new Error('done() called multiple times'));
});
var timeout = this.getTimeout();
var promiseWithTimeout = Promise.race([promise, throwAfterTimeout(timeout), this.getRunner().getUncaughtPromise()]);
return promiseWithTimeout.catch(function (e) {
_this2.failCount += 1;
if (_this2.failCount <= _this2.getRetryCount()) {
return _this2.run();
}
throw e;
});
}
}]);
return TestRunnableNode;
}(TestNode);
module.exports = TestRunnableNode;
;