I have a class method like this:
export class MyClass {
@myDecorator()
public async createItem(itemId: string, itemOptions: ItemOption[]): Promise<boolean> {
// ...
// return await create Item
}
}
I defined the decorator in another file like this:
export function myDecorator() {
return function check(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const newDescriptor = Object.assign({}, descriptor);
// do something ...
return newDescriptor;
}
}
Now I want to unit test the createItem method not the decorator, only the method. But as the decorator is kind of heavy in what it does (there is even a database access in it) I need to mock it so that it doesn't get executed in the unit test.
I'm using mocha and sinon in my unit tests and already tried the following to mock the decorator:
const decorator = require('./decorator/MyDecorator');
describe('test createItem method', function () {
it('should return valid result', async function () {
// 1. try
decorator.myDecorator = () => sinon.fake();
// 2. try
const decoratorStub = sinon.stub(decorator, 'myDecorator');
decoratorStub.return(true);
// rest of test
const myClass = new MyClass();
let result = await myClass.createItem(itemId, options);
expect(result).to.be.equal(true);
}
}
Both of the tries were not successfull. the decorator was executed anyways but I actually just want to skip it.
How can I achieve this?