UNPKG

@tsdi/unit

Version:

unit testing framework, base on AOP, Ioc container

351 lines (349 loc) 14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OldTestRunner = void 0; var tslib_1 = require("tslib"); var ioc_1 = require("@tsdi/ioc"); var assert_1 = require("../assert/assert"); var gls = { describe: undefined, suite: undefined, it: undefined, test: undefined, before: undefined, beforeAll: undefined, beforeEach: undefined, after: undefined, afterAll: undefined, afterEach: undefined }; var testkeys = Object.keys(gls); var globals = typeof window !== 'undefined' ? window : global; /** * Suite runner. * * @export * @class SuiteRunner * @implements {IRunner<any>} */ var OldTestRunner = /** @class */ (function (_super) { tslib_1.__extends(OldTestRunner, _super); function OldTestRunner(timeout) { var _this = _super.call(this) || this; _this.suites = []; _this.timeout = timeout || (3 * 60 * 60 * 1000); return _this; } OldTestRunner.prototype.configureService = function (ctx) { return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { return [2 /*return*/]; }); }); }; OldTestRunner.prototype.getContext = function () { return null; }; OldTestRunner.prototype.getBootType = function () { return null; }; OldTestRunner.prototype.getBoot = function () { return this.suites; }; OldTestRunner.prototype.registerGlobalScope = function () { // isUndefined(window) ? global : window; testkeys.forEach(function (k) { gls[k] = globals[k]; }); var suites = this.suites; // BDD style var describe = globals.describe = function (name, fn, superDesc) { if (!ioc_1.isFunction(fn)) return; var suiteDesc = tslib_1.__assign(tslib_1.__assign({}, superDesc), { describe: name, cases: [] }); suites.push(suiteDesc); globals.describe = function (subname, fn) { describe(name + ' ' + subname, fn, suiteDesc); }; globals.it = function (title, test, timeout) { if (!ioc_1.isFunction(test)) return; suiteDesc.cases.push({ title: title, key: '', fn: test, timeout: timeout }); }; globals.before = globals.beforeAll = function (fn, timeout) { if (!ioc_1.isFunction(fn)) return; suiteDesc.before = suiteDesc.before || []; suiteDesc.before.push({ fn: fn, timeout: timeout }); }; globals.beforeEach = function (fn, timeout) { if (!ioc_1.isFunction(fn)) return; suiteDesc.beforeEach = suiteDesc.beforeEach || []; suiteDesc.beforeEach.push({ fn: fn, timeout: timeout }); }; globals.after = globals.afterAll = function (fn, timeout) { if (!ioc_1.isFunction(fn)) return; suiteDesc.after = suiteDesc.after || []; suiteDesc.after.push({ fn: fn, timeout: timeout }); }; globals.afterEach = function (fn, timeout) { if (!ioc_1.isFunction(fn)) return; suiteDesc.afterEach = suiteDesc.afterEach || []; suiteDesc.afterEach.push({ fn: fn, timeout: timeout }); }; fn && fn(); globals.describe = describe; }; // TDD style var suite = globals.suite = function (name, fn, superDesc) { var suiteDesc = tslib_1.__assign(tslib_1.__assign({}, superDesc), { describe: name, cases: [] }); suites.push(suiteDesc); globals.suite = function (subname, fn) { suite(name + ' ' + subname, fn, suiteDesc); }; globals.test = function (title, test, timeout) { suiteDesc.cases.push({ title: title, key: '', fn: test, timeout: timeout }); }; globals.before = globals.beforeAll = function (test, timeout) { suiteDesc.before = suiteDesc.before || []; suiteDesc.before.push({ fn: test, timeout: timeout }); }; globals.beforeEach = function (test, timeout) { suiteDesc.beforeEach = suiteDesc.beforeEach || []; suiteDesc.beforeEach.push({ fn: test, timeout: timeout }); }; globals.after = globals.afterAll = function (test, timeout) { suiteDesc.after = suiteDesc.after || []; suiteDesc.after.push({ fn: test, timeout: timeout }); }; globals.afterEach = function (test, timeout) { suiteDesc.afterEach = suiteDesc.afterEach || []; suiteDesc.afterEach.push({ fn: test, timeout: timeout }); }; fn && fn(); globals.suite = suite; }; }; OldTestRunner.prototype.unregisterGlobalScope = function () { // reset to default. testkeys.forEach(function (k) { globals[k] = gls[k]; }); }; OldTestRunner.prototype.startup = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { return [2 /*return*/, this.run()]; }); }); }; OldTestRunner.prototype.run = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var err_1; var _this = this; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, ioc_1.PromiseUtil.step(this.suites.map(function (desc) { return desc.cases.length ? function () { return _this.runSuite(desc); } : function () { return Promise.resolve(); }; }))]; case 1: _a.sent(); return [3 /*break*/, 3]; case 2: err_1 = _a.sent(); return [3 /*break*/, 3]; case 3: return [2 /*return*/]; } }); }); }; OldTestRunner.prototype.runSuite = function (desc) { return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.runBefore(desc)]; case 1: _a.sent(); return [4 /*yield*/, this.runTest(desc)]; case 2: _a.sent(); return [4 /*yield*/, this.runAfter(desc)]; case 3: _a.sent(); return [2 /*return*/]; } }); }); }; OldTestRunner.prototype.runTimeout = function (fn, describe, timeout) { var _this = this; var defer = ioc_1.PromiseUtil.defer(); var timer = setTimeout(function () { if (timer) { clearTimeout(timer); var assert = _this.injector.resolve(assert_1.Assert); var err = new assert.AssertionError({ message: describe + ", timeout " + timeout, stackStartFunction: fn, stackStartFn: fn }); defer.reject(err); } }, timeout || this.timeout); Promise.resolve(fn(function () { return defer.resolve(); })) .then(function (r) { clearTimeout(timer); timer = null; defer.resolve(r); }) .catch(function (err) { clearTimeout(timer); timer = null; defer.reject(err); }); return defer.promise; }; OldTestRunner.prototype.runHook = function (describe, action, desc) { return tslib_1.__awaiter(this, void 0, void 0, function () { var _this = this; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, ioc_1.PromiseUtil.step((describe[action] || []) .map(function (hk) { return function () { return _this.runTimeout(hk.fn, desc, hk.timeout || describe.timeout); }; }))]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; OldTestRunner.prototype.runBefore = function (describe) { return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.runHook(describe, 'before', 'suite before')]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; OldTestRunner.prototype.runBeforeEach = function (describe) { return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.runHook(describe, 'beforeEach', 'before each')]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; OldTestRunner.prototype.runAfterEach = function (describe) { return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.runHook(describe, 'afterEach', 'after case each')]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; OldTestRunner.prototype.runAfter = function (describe) { return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.runHook(describe, 'after', 'suite after')]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; OldTestRunner.prototype.runTest = function (desc) { return tslib_1.__awaiter(this, void 0, void 0, function () { var _this = this; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, ioc_1.PromiseUtil.step(desc.cases.map(function (caseDesc) { return function () { return _this.runCase(caseDesc, desc); }; }))]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; OldTestRunner.prototype.runCase = function (caseDesc, suiteDesc) { return tslib_1.__awaiter(this, void 0, void 0, function () { var err_2; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 4, , 5]); return [4 /*yield*/, this.runBeforeEach(suiteDesc)]; case 1: _a.sent(); return [4 /*yield*/, this.runTimeout(caseDesc.fn, caseDesc.title, caseDesc.timeout)]; case 2: _a.sent(); return [4 /*yield*/, this.runAfterEach(suiteDesc)]; case 3: _a.sent(); return [3 /*break*/, 5]; case 4: err_2 = _a.sent(); caseDesc.error = err_2; return [3 /*break*/, 5]; case 5: return [2 /*return*/, caseDesc]; } }); }); }; OldTestRunner.prototype.destroying = function () { }; OldTestRunner.ρAnn = function () { return { "name": "OldTestRunner", "params": { "configureService": ["ctx"], "constructor": ["timeout"], "runSuite": ["desc"], "runTimeout": ["fn", "describe", "timeout"], "runHook": ["describe", "action", "desc"], "runBefore": ["describe"], "runBeforeEach": ["describe"], "runAfterEach": ["describe"], "runAfter": ["describe"], "runTest": ["desc"], "runCase": ["caseDesc", "suiteDesc"] } }; }; tslib_1.__decorate([ ioc_1.Inject(ioc_1.INJECTOR), tslib_1.__metadata("design:type", Object) ], OldTestRunner.prototype, "injector", void 0); OldTestRunner = tslib_1.__decorate([ ioc_1.Singleton, tslib_1.__metadata("design:paramtypes", [Number]) ], OldTestRunner); return OldTestRunner; }(ioc_1.Destoryable)); exports.OldTestRunner = OldTestRunner; //# sourceMappingURL=../sourcemaps/runner/OldTestRunner.js.map