...alternative is resolves
assertion.
describe("Foo.bar()", () => {
it("should not throw", () => {
return expect(new Foo().bar()).resolves.toEqual();
});
});
Here you have to return result since it's a Promise(and make jest wait until it's fulfilled). It's slightly more laconic if you need just verify resolved(or rejected - there is similar prop rejects
for checking rejection value).
But in case you need to run several checks after promise-based function is run like
describe("Foo.bar()", () => {
it("should not throw", () => {
const promise = new Foo().bar()
expect(promise).resolves.toEqual();
expect(someMock).toHaveBeenCalled();
return promise;
});
});
you may find option with async/await
is more... satisfying?
PS as for you variant
describe("Foo.bar()", () => {
it("should not throw", async () => {
expect.assertions(1);
try {
await new Foo().bar();
expect(true).toBeTruthy();
} catch {
// should not come here
}
});
});
I believe it's not needed to catch an error - so expect.assertions
also becomes redundant. why? uncaught exception will fail your test and it's expected no exception so it's fine to fail it. such a structure will be needed if you expect exception and want to check its properties.
Also if test fails option with expect.assertions
will notify you just about it's failed while uncaught exception will highlight specific statement(useful if test has several exception-possible statements)
[UPD] also I missed initial point that you need to check if promise is resolved with any result(my version check it resolves with undefined
that is not really good move(it does not ruin anything if function starts returning something if it returned nothing before). Meanwhile we should have at least one check in test...
So maybe your approach with stub expect(true)
is the same legit here.
But I'd verify twice if you don't want to make more expect
in the same test case. is that statement under test really such isolated? or you just explode related checks into separate it()
?