[M0086] Motoko Wasm Async Functions

Is there any chance to get Motoko to support async functions when compiling to standalone wasm? Even if the calls aren’t truly async.

We’re trying to mock calling an external service but have hit a wall because we can’t write even pretend async functions.

Little example:

public type ExternalServiceCaller<T, V> = (T, V) -> async ();

let mockCaller : ExternalServiceCaller<shared () -> async (), ()> = func (f, _) : async () {
 //Noop. Maybe increment counter or capture args.. 

let realCaller : ExternalServiceCaller<shared () -> async (), ()> = func (f, _) : async () {
 ignore f();
 // Call service.. Tick call counter... something else

Could you help me understand why you’re compiling to standalone Wasm?

For unit testing.


moc $(shell vessel sources) -wasi-system-api -o Test.wasm test/*unit_test_runner.mo && wasmtime test.wasm
rm -f test.wasm

I used to just use moc -r <filepath>.

Can you do that instead?

1 Like

I guess the interpreter was all I had back then, and may not be close enough to the real environment.

:woman_facepalming: yes this solves the problem lol. Good call, thank you!

1 Like

As long as you’re comfortable testing interpreted code rather than compiled code :slightly_smiling_face:

We don’t really have the resources to extend the wasi backend to support faux actors, although that would be a cool project.

Motoko matchers had some rudimentary support for unit testing canisters https://kritzcreek.github.io/motoko-matchers/Canister.html
but that doesn’t solve your mocking problem.

I wonder if you could just write the canister under test as a class, parameterised by the mock/real code and then write another test harness that instantiates it with the mock and runs some tests and run both on the local replica or real IC.

Alternatively, ic-repl might be useful for the testing process.

Thank you @claudio - the interpreter has been working fine so far. Going to look into ic-repl!

I did hit one loosely related bug : https://github.com/dfinity/motoko/issues/3096

1 Like

Hi @Hazel

I’d be very curious to know what your current testing flow is? It’s not so easy to understand how to do that well as a beginner. I’m trying to understand how the leading devs in the ecosystem are doing it.

Thanks in advance for any advice!