UNPKG

@tsdi/unit

Version:

unit testing framework, base on AOP, Ioc container

132 lines (130 loc) 3.71 kB
import { createClassDecorator, isString, isNumber, createMethodDecorator, isArray } from '@tsdi/ioc'; /** * create filed decorator. * * @export * @template T * @param {string} [SuiteType] * @param {ArgsIteratorAction<T>[]} [actions] * @param {MetadataExtends<T>} [metaExtends] * @returns {IFiledDecorator<T>} */ export function createSuiteDecorator(actions, metaExtends) { return createClassDecorator('Suite', [ ...(actions || []), (ctx, next) => { let arg = ctx.currArg; if (isString(arg)) { ctx.metadata.describe = arg; ctx.next(next); } }, (ctx, next) => { let arg = ctx.currArg; if (isNumber(arg)) { ctx.metadata.timeout = arg; ctx.next(next); } } ], (metadata) => { if (metaExtends) { metaExtends(metadata); } metadata.singleton = true; return metadata; }); } export const Suite = createSuiteDecorator(); /** * create Test decorator. * * @export * @template T * @param {string} [TestType] * @param {MetadataAdapter} [actions] * @param {MetadataExtends<T>} [metaExtends] * @returns {ITestDecorator<T>} */ export function createTestDecorator(name, actions, finallyActions, metaExtends) { return createMethodDecorator(name, [ ...(actions ? (isArray(actions) ? actions : [actions]) : []), (ctx, next) => { let arg = ctx.currArg; if (isNumber(arg)) { ctx.metadata.timeout = arg; ctx.next(next); } }, ...(finallyActions ? (isArray(finallyActions) ? finallyActions : [finallyActions]) : []) ], metaExtends); } /** * @Test decorator. define the method of class as unit test case. Describe a specification or test-case with the given `title` and callback `fn` acting * as a thunk. * * @export * @interface ITestDecorator * @template T */ export const Test = createTestDecorator('TestCase', (ctx, next) => { let arg = ctx.currArg; if (isString(arg)) { ctx.metadata.title = arg; ctx.next(next); } }, (ctx, next) => { let arg = ctx.currArg; if (isNumber(arg)) { ctx.metadata.setp = arg; ctx.next(next); } }); /** * @BeforeAll decorator. define the method of class as unit test action run before all test case. * * @export * @interface IBeforeTestDecorator * @template T */ export const BeforeAll = createTestDecorator('BeforeAll'); /** * @BeforeAll decorator. define the method of class as unit test action run before all test case. * * @export * @interface IBeforeTestDecorator * @template T */ export const Before = BeforeAll; /** * @BeforeEach decorator. define the method of class as unit test action run before each test case. * * @export * @interface IBeforeEachTestDecorator * @template T */ export const BeforeEach = createTestDecorator('TestBeforeEach'); /** * @AfterAll decorator. define the method of class as unit test action run after all test case. * * @export * @interface IAfterTestDecorator * @template T */ export const AfterAll = createTestDecorator('AfterAll'); /** * @AfterAll decorator. define the method of class as unit test action run after all test case. * * @export * @interface IAfterTestDecorator * @template T */ export const After = AfterAll; /** * @AfterEach decorator. define the method of class as unit test action run after each test case. * * @export * @interface IAfterEachTestDecorator * @template T */ export const AfterEach = createTestDecorator('TestAfterEach'); //# sourceMappingURL=sourcemaps/decorators.js.map