We are testing a call to a function that returns a Promise<void>
. How can we wait until the promise resolves and then
do some test?
The standard approach of then()
off of the function does not work, because a Promise<void>
is not thenable. So we have resorted to the following, which works but seems non-ideal.
Initial Approach
it("it does something...", function (done) {
function wrappedPromise() {
functionThatReturnsPromiseVoid(someArg);
return new Promise((resolve) => resolve());
}
wrappedPromise()
.then(() => {
expect(someVar).toBe("someValue");
done();
});
});
Subsequent Approach
let wrapVoidPromise = (wrapped): Promise<any> => new Promise((resolve) => {
wrapped();
resolve();
});
it("it does something...", function (done) {
wrapVoidPromise(() => functionThatReturnsPromiseVoid(someArg))
.then(() => {
expect(someVar).toBe("someValue");
done();
});
});
How can we do this without needing to wrap the unchainable promise?
Here is a GitHub link to the actual test . It includes this call:
route.navigationStrategy(instruction) // returns Promise<void>
Here is a GitHub link to the function . The implementation is buried somewhere inside the source to aurelia-router. Here is its interface:
navigationStrategy?: (instruction: NavigationInstruction) => Promise<void>|void;
You may try changing wrapUnchainedPromise as below :
function wrapUnchainedPromise(): Promise<any> {
return route.navigationStrategy(instruction);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.