/********************************************************************* * * * 单元测试装饰器 * 创建时间: 2021-06-02 * 作者: 孟凯伦 * * **********************************************************************/ import { Done } from "mocha"; type TestActionItemType = { title: string, fn?: (...args: any[]) => Promise }; type TestInstanceType = { constructor: { name: string, __stack_to_test__?: TestActionItemType[] }, [key: string]: any }; /** * 测试单元装饰器 * @param actionDescription 单元描述 */ export function Test(actionDescription: string) : (target: T, functionName: string) => void { return (target: T, functionName: string): void => { // 创建测试堆栈 if (target.constructor.__stack_to_test__ === undefined) { target.constructor.__stack_to_test__ = new Array(); } // 添加到测试堆栈中 target.constructor.__stack_to_test__.push({ title: actionDescription, fn: target[functionName].bind(target) }); } } /** * 测试基类装饰器(内装饰器在属性装饰器后执行) */ export function Tests(moduleDescription: string) : (target: T) => void { return (target: T): void => { // 定义执行模块测试 describe(moduleDescription, () => { // 变量测试单元列表 target.__stack_to_test__?.forEach((item: TestActionItemType) => { it(item.title, async () => { if (item.fn === undefined) { return; } await item.fn(); return "测试成功!" }); }); }); } }