Hazel
February 2, 2022, 3:26pm
1
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?
Hazel
February 2, 2022, 6:29pm
3
For unit testing.
Like
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.
Hazel
February 2, 2022, 6:46pm
6
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
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.
Hazel
February 4, 2022, 5:07pm
9
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
Mathias
December 12, 2022, 12:11pm
10
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!