@tsdi/unit
Version:
unit testing framework, base on AOP, Ioc container
261 lines (259 loc) • 9.08 kB
JavaScript
import { __awaiter, __decorate, __metadata } from "tslib";
import { Inject, PromiseUtil, Singleton, INJECTOR, isFunction, Destoryable } from '@tsdi/ioc';
import { Assert } from '../assert/assert';
const gls = {
describe: undefined,
suite: undefined,
it: undefined,
test: undefined,
before: undefined,
beforeAll: undefined,
beforeEach: undefined,
after: undefined,
afterAll: undefined,
afterEach: undefined
};
const testkeys = Object.keys(gls);
const globals = typeof window !== 'undefined' ? window : global;
/**
* Suite runner.
*
* @export
* @class SuiteRunner
* @implements {IRunner<any>}
*/
let OldTestRunner = class OldTestRunner extends Destoryable {
constructor(timeout) {
super();
this.suites = [];
this.timeout = timeout || (3 * 60 * 60 * 1000);
}
configureService(ctx) {
return __awaiter(this, void 0, void 0, function* () {
});
}
getContext() {
return null;
}
getBootType() {
return null;
}
getBoot() {
return this.suites;
}
registerGlobalScope() {
// isUndefined(window) ? global : window;
testkeys.forEach(k => {
gls[k] = globals[k];
});
let suites = this.suites;
// BDD style
let describe = globals.describe = (name, fn, superDesc) => {
if (!isFunction(fn))
return;
let suiteDesc = Object.assign(Object.assign({}, superDesc), { describe: name, cases: [] });
suites.push(suiteDesc);
globals.describe = (subname, fn) => {
describe(name + ' ' + subname, fn, suiteDesc);
};
globals.it = (title, test, timeout) => {
if (!isFunction(test))
return;
suiteDesc.cases.push({ title: title, key: '', fn: test, timeout: timeout });
};
globals.before = globals.beforeAll = (fn, timeout) => {
if (!isFunction(fn))
return;
suiteDesc.before = suiteDesc.before || [];
suiteDesc.before.push({
fn: fn,
timeout: timeout
});
};
globals.beforeEach = (fn, timeout) => {
if (!isFunction(fn))
return;
suiteDesc.beforeEach = suiteDesc.beforeEach || [];
suiteDesc.beforeEach.push({
fn: fn,
timeout: timeout
});
};
globals.after = globals.afterAll = (fn, timeout) => {
if (!isFunction(fn))
return;
suiteDesc.after = suiteDesc.after || [];
suiteDesc.after.push({
fn: fn,
timeout: timeout
});
};
globals.afterEach = (fn, timeout) => {
if (!isFunction(fn))
return;
suiteDesc.afterEach = suiteDesc.afterEach || [];
suiteDesc.afterEach.push({
fn: fn,
timeout: timeout
});
};
fn && fn();
globals.describe = describe;
};
// TDD style
let suite = globals.suite = function (name, fn, superDesc) {
let suiteDesc = Object.assign(Object.assign({}, superDesc), { describe: name, cases: [] });
suites.push(suiteDesc);
globals.suite = (subname, fn) => {
suite(name + ' ' + subname, fn, suiteDesc);
};
globals.test = (title, test, timeout) => {
suiteDesc.cases.push({ title: title, key: '', fn: test, timeout: timeout });
};
globals.before = globals.beforeAll = (test, timeout) => {
suiteDesc.before = suiteDesc.before || [];
suiteDesc.before.push({
fn: test,
timeout: timeout
});
};
globals.beforeEach = (test, timeout) => {
suiteDesc.beforeEach = suiteDesc.beforeEach || [];
suiteDesc.beforeEach.push({
fn: test,
timeout: timeout
});
};
globals.after = globals.afterAll = (test, timeout) => {
suiteDesc.after = suiteDesc.after || [];
suiteDesc.after.push({
fn: test,
timeout: timeout
});
};
globals.afterEach = (test, timeout) => {
suiteDesc.afterEach = suiteDesc.afterEach || [];
suiteDesc.afterEach.push({
fn: test,
timeout: timeout
});
};
fn && fn();
globals.suite = suite;
};
}
unregisterGlobalScope() {
// reset to default.
testkeys.forEach(k => {
globals[k] = gls[k];
});
}
startup() {
return __awaiter(this, void 0, void 0, function* () {
return this.run();
});
}
run() {
return __awaiter(this, void 0, void 0, function* () {
try {
yield PromiseUtil.step(this.suites.map(desc => desc.cases.length ? () => this.runSuite(desc) : () => Promise.resolve()));
}
catch (err) {
// console.error(err);
}
});
}
runSuite(desc) {
return __awaiter(this, void 0, void 0, function* () {
yield this.runBefore(desc);
yield this.runTest(desc);
yield this.runAfter(desc);
});
}
runTimeout(fn, describe, timeout) {
let defer = PromiseUtil.defer();
let timer = setTimeout(() => {
if (timer) {
clearTimeout(timer);
let assert = this.injector.resolve(Assert);
let err = new assert.AssertionError({
message: `${describe}, timeout ${timeout}`,
stackStartFunction: fn,
stackStartFn: fn
});
defer.reject(err);
}
}, timeout || this.timeout);
Promise.resolve(fn(() => defer.resolve()))
.then(r => {
clearTimeout(timer);
timer = null;
defer.resolve(r);
})
.catch(err => {
clearTimeout(timer);
timer = null;
defer.reject(err);
});
return defer.promise;
}
runHook(describe, action, desc) {
return __awaiter(this, void 0, void 0, function* () {
yield PromiseUtil.step((describe[action] || [])
.map(hk => () => this.runTimeout(hk.fn, desc, hk.timeout || describe.timeout)));
});
}
runBefore(describe) {
return __awaiter(this, void 0, void 0, function* () {
yield this.runHook(describe, 'before', 'suite before');
});
}
runBeforeEach(describe) {
return __awaiter(this, void 0, void 0, function* () {
yield this.runHook(describe, 'beforeEach', 'before each');
});
}
runAfterEach(describe) {
return __awaiter(this, void 0, void 0, function* () {
yield this.runHook(describe, 'afterEach', 'after case each');
});
}
runAfter(describe) {
return __awaiter(this, void 0, void 0, function* () {
yield this.runHook(describe, 'after', 'suite after');
});
}
runTest(desc) {
return __awaiter(this, void 0, void 0, function* () {
yield PromiseUtil.step(desc.cases.map(caseDesc => () => this.runCase(caseDesc, desc)));
});
}
runCase(caseDesc, suiteDesc) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield this.runBeforeEach(suiteDesc);
yield this.runTimeout(caseDesc.fn, caseDesc.title, caseDesc.timeout);
yield this.runAfterEach(suiteDesc);
}
catch (err) {
caseDesc.error = err;
}
return caseDesc;
});
}
destroying() {
}
static ρAnn() {
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"] } };
}
};
__decorate([
Inject(INJECTOR),
__metadata("design:type", Object)
], OldTestRunner.prototype, "injector", void 0);
OldTestRunner = __decorate([
Singleton,
__metadata("design:paramtypes", [Number])
], OldTestRunner);
export { OldTestRunner };
//# sourceMappingURL=../sourcemaps/runner/OldTestRunner.js.map