smock
smock copied to clipboard
feature request: support async function when mocking return
Is your feature request related to a problem? Please describe. I was not able to pass an async function in order to return a dynamic value, only when the function is sync it works fine.
Describe the solution you'd like
myFake.myFunction.returns(async () => { ... })
+1
I can confirm this doesn't work, if you modify the async test case here: https://github.com/defi-wonderland/smock/blob/main/test/unit/programmable-function-logic/type-handling.spec.ts#L237 to something like
it('should wait for result as promise', async () => {
const asyncRequest = (n: number) => {
return new Promise(function (resolve) {
setTimeout(() => {
resolve(n * 10);
}, 10)
});
};
fake.getInputtedUint256.returns(async (number: number) => asyncRequest(number));
expect(await fake.callStatic.getInputtedUint256(10)).to.equal(100);
});
it will fail. I believe this is an issue with performing an async action within RxJS's .subscribe
but I'm not sure what the right fix is.
I created some tests some time ago to also demonstrate the issue. You can find them here: https://github.com/defi-wonderland/smock/pull/143