defi-wonderland / smock

The Solidity mocking library
MIT License
319 stars 40 forks source link

feature request: support async function when mocking return #107

Open marcelomorgado opened 2 years ago

marcelomorgado commented 2 years ago

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 () => { ... })
kbrizzle commented 2 years ago

+1

arjun-io commented 2 years ago

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.

robercano commented 1 year ago

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