I am setting up some asynchronous junit
typescript tests and it seems I don't understand the documentation correctly.
I wrote a test asking a web API for a specific svg image by sending the ID of the image.
The test sends no ID so the web API should return http code 404. This the test and it works well:
test("getSvgByImageId unknown", () => {
expect.assertions(1);
return client.getSvgImageById("")
.then(svg => {
fail("API should return error")
})
.catch(error => {
expect(error.status).toBe(404);
})
});
But why should I use the expect.assertions(x) when I use the fail() method? The test also works without the expect.assertions(x) line.
I am setting up some asynchronous junit
typescript tests and it seems I don't understand the documentation correctly.
I wrote a test asking a web API for a specific svg image by sending the ID of the image.
The test sends no ID so the web API should return http code 404. This the test and it works well:
test("getSvgByImageId unknown", () => {
expect.assertions(1);
return client.getSvgImageById("")
.then(svg => {
fail("API should return error")
})
.catch(error => {
expect(error.status).toBe(404);
})
});
But why should I use the expect.assertions(x) when I use the fail() method? The test also works without the expect.assertions(x) line.
Share Improve this question edited Jan 18, 2019 at 9:14 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Jan 17, 2019 at 6:50 FleMoFleMo 5651 gold badge6 silver badges21 bronze badges 1- 1 Two documentation pages really helped me: jestjs.io/docs/en/asynchronous and jestjs.io/docs/en/tutorial-async – Sixty4Bit Commented Mar 2, 2021 at 20:40
1 Answer
Reset to default 7From this post by a Jest
collaborator in reference to the global fail()
function:
That will stop working at some point - it's not part of Jest's documented API.
Jest
is based on Jasmine
and fail()
is a carryover from Jasmine
.
It is not officially part of the Jest
documentation so it should not be used as it could be removed from future Jest
releases.