In vitest you can expect a Promise to be resolved using this code:
await expect(expected, "custom message").resolves.toBeUndefined()
However, when failing to resolve, the custom message is not shown:
promise rejected "undefined" instead of resolving
How to display this custom message when the expect fails?
In vitest you can expect a Promise to be resolved using this code:
await expect(expected, "custom message").resolves.toBeUndefined()
However, when failing to resolve, the custom message is not shown:
promise rejected "undefined" instead of resolving
How to display this custom message when the expect fails?
Share Improve this question edited Feb 6 at 9:51 Blee asked Feb 6 at 8:53 BleeBlee 6477 silver badges13 bronze badges 01 Answer
Reset to default 0I ended up adding a little util that catches the error and throws the custom message:
export async function expectToResolve(expected: Promise<any>, msg: string) {
try {
await expect(expected).resolves.toBeUndefined()
} catch (cause) {
throw new Error(msg, {cause})
}
}
It can be used like this:
await expectToResolve(promise, "custom message");