kocha
Version:
Modern, simpler Mocha clone, no globals, lint friendly
140 lines (113 loc) • 5.16 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; }; }();
var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
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 TestSuite = require('./test-suite');
var EVENT_UNCAUGHT_EXCEPTION = 'uncaughtException';
/**
* The test runner class.
*
* Events:
* - `start` execution started
* - `end` execution complete
* - `suite` (suite) test suite execution started
* - `suite end` (suite) all tests (and sub-suites) have finished
* - `test` (test) test execution started
* - `test end` (test) test completed
* - `hook` (hook) hook execution started
* - `hook end` (hook) hook complete
* - `pass` (test) test passed
* - `fail` (test, err) test failed
* - `pending` (test) test pending
*/
var TestRunner = function (_TestSuite) {
_inherits(TestRunner, _TestSuite);
function TestRunner() {
_classCallCheck(this, TestRunner);
var _this = _possibleConstructorReturn(this, (TestRunner.__proto__ || Object.getPrototypeOf(TestRunner)).call(this, 'root', false, null));
_this.timeoutDuration = 2000;
_this.currentSuite = _this;
_this.runningNode = null;
return _this;
}
/**
* Returns the current suite.
* @return {TestSuite}
*/
_createClass(TestRunner, [{
key: 'getCurrentSuite',
value: function getCurrentSuite() {
return this.currentSuite;
}
/**
* Sets the current suite of the runner.
* @param {TestSuite}
*/
}, {
key: 'setCurrentSuite',
value: function setCurrentSuite(suite) {
this.currentSuite = suite;
}
}, {
key: 'getCurrentNode',
value: function getCurrentNode() {
return this.getRunningNode() || this.getCurrentSuite();
}
/**
* Sets the currently running node.
* @param {TestRunnableNode} test The runnable node
*/
}, {
key: 'setRunningNode',
value: function setRunningNode(runnable) {
this.runningNode = runnable;
}
/**
* Gets the currently running test case.
* @return {TestRunnableNode}
*/
}, {
key: 'getRunningNode',
value: function getRunningNode() {
return this.runningNode;
}
}, {
key: 'getUncaughtPromise',
value: function getUncaughtPromise() {
return this.uncaughtPromise;
}
}, {
key: 'bindToUncaughtException',
value: function bindToUncaughtException() {
var _this2 = this;
this.uncaughtPromise = new Promise(function (resolve, reject) {
var uncaughtListener = function uncaughtListener(e) {
process.removeListener(EVENT_UNCAUGHT_EXCEPTION, uncaughtListener);
_this2.bindToUncaughtException();
e.uncaught = true;
reject(e);
};
process.on(EVENT_UNCAUGHT_EXCEPTION, uncaughtListener);
});
}
/**
* Runs the tests. Private API.
* @return {Promise}
*/
}, {
key: 'run',
value: function run() {
var _this3 = this;
this.bindToUncaughtException();
this.total = this.getTotal();
this.bubbleEvent('start');
return _get(TestRunner.prototype.__proto__ || Object.getPrototypeOf(TestRunner.prototype), 'run', this).call(this).then(function () {
return _this3.bubbleEvent('end');
});
}
}]);
return TestRunner;
}(TestSuite);
module.exports = TestRunner;
;