UNPKG

kocha

Version:

Modern, simpler Mocha clone, no globals, lint friendly

209 lines (175 loc) 5.88 kB
'use strict'; 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 TestSuite = function (_TestNode) { _inherits(TestSuite, _TestNode); /** * @param {string} title The title of the test suite * @param {boolean} skipped True iff the suite is skipped * @param {Suite} parent The parent suite */ function TestSuite(title, skipped, parent) { _classCallCheck(this, TestSuite); var _this = _possibleConstructorReturn(this, (TestSuite.__proto__ || Object.getPrototypeOf(TestSuite)).call(this, title, skipped, parent)); _this.beforeHook = null; _this.beforeEachHook = null; _this.afterHook = null; _this.afterEachHook = null; _this.tests = []; _this.suites = []; return _this; } /** * Gets the total count of the tests. * @return {number} */ _createClass(TestSuite, [{ key: 'getTotal', value: function getTotal() { return this.tests.length + this.suites.reduce(function (sum, suite) { return sum + suite.getTotal(); }, 0); } /** * @param {TestHook} hook */ }, { key: 'setBeforeHook', value: function setBeforeHook(hook) { this.beforeHook = hook; } /** * @param {TestHook} hook */ }, { key: 'setBeforeEachHook', value: function setBeforeEachHook(hook) { this.beforeEachHook = hook; } /** * @param {TestHook} hook */ }, { key: 'setAfterEachHook', value: function setAfterEachHook(hook) { this.afterEachHook = hook; } /** * @param {TestHook} hook */ }, { key: 'setAfterHook', value: function setAfterHook(hook) { this.afterHook = hook; } /** * Adds the test suite. * @param {Suite} suite The test suite to add */ }, { key: 'addSuite', value: function addSuite(suite) { this.suites.push(suite); } /** * Adds the test case. * @param {Test} test The test case to add */ }, { key: 'addTest', value: function addTest(test) { this.tests.push(test); } }, { key: 'runBeforeHook', value: function runBeforeHook() { return this.beforeHook ? this.beforeHook.run() : Promise.resolve(true); } }, { key: 'runBeforeEachHooks', value: function runBeforeEachHooks() { var _this2 = this; var promise = Promise.resolve(); if (this.parent) { promise = this.parent.runBeforeEachHooks(); } return promise.then(function () { return _this2.beforeEachHook ? _this2.beforeEachHook.run() : Promise.resolve(true); }); } }, { key: 'runAfterHook', value: function runAfterHook() { return this.afterHook ? this.afterHook.run() : Promise.resolve(true); } }, { key: 'runAfterEachHooks', value: function runAfterEachHooks() { var _this3 = this; var promise = this.afterEachHook ? this.afterEachHook.run() : Promise.resolve(true); if (this.parent) { promise = promise.then(function () { return _this3.parent.runAfterEachHooks(); }); } return promise; } }, { key: 'start', value: function start() { if (!this.root) { this.bubbleEvent('suite', this); } } }, { key: 'end', value: function end() { if (!this.root) { this.bubbleEvent('suite end', this); } } }, { key: 'run', value: function run() { var _this4 = this; this.start(); return this.runBeforeHook().then(function (passed) { if (!passed) { return; } return _this4.runTests().then(function () { return _this4.runSuites(); }); }).then(function () { return _this4.runAfterHook(); }).then(function () { return _this4.end(); }, function () { return _this4.end(); }); } }, { key: 'runTests', value: function runTests() { return this.tests.reduce(function (prev, test) { return prev.then(function () { return test.run(); }); }, Promise.resolve()); } }, { key: 'runSuites', value: function runSuites() { return this.suites.reduce(function (prev, suite) { return prev.then(function () { return suite.run(); }); }, Promise.resolve()); } }]); return TestSuite; }(TestNode); module.exports = TestSuite;