我有一个 typescript 类:
export class SystemUnderTest {
@LogThisAction('sth was done')
public doSomething() {}
}
如您所见,它使用反射来执行一些装饰功能:
export declare function LogThisAction(action: string): (target: any) =>
void;
当我运行测试时,我不关心实际的实现。这个装饰器函数,所以我试着像这样模拟它:
myModule = require(./DecoratorFunctions);
myModule.LogThisAction = jest.fn();
但这似乎行不通。当我运行测试时,我得到:
● Test suite failed to run
TypeError: decorator is not a function
at DecorateProperty (node_modules/reflect-metadata/Reflect.js:553:33)
如何在 JEST 框架中实现我的目标?
最佳答案
从技术上讲,您的装饰器是一个返回另一个函数的函数。
所以你的模拟是不正确的,它应该返回一个函数,试试看:
myModule = require(./DecoratorFunctions);
myModule.LogThisAction = () => jest.fn();
关于javascript - 如何模拟 JEST 中 SUT 中使用的方法上使用的装饰器函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53960426/