admin管理员组

文章数量:1405544

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
Add a ment  | 

1 Answer 1

Reset to default 7

From 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.

本文标签: javascriptUsing expectassertions(x) vs using fail() in jest testsStack Overflow