最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using expect.assertions(x) vs using fail() in jest tests - Stack Overflow

programmeradmin9浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论