开玩笑:(仅)单个测试后如何拆卸 [英] jest: How to teardown after (just) an individual test

查看:66
本文介绍了开玩笑:(仅)单个测试后如何拆卸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jest提供afterEachbeforeEachafterAllbeforeAll来完成设置和拆卸逻辑.我想做的是在一项特定的测试之后进行清理.请考虑以下内容:

jest provides afterEach, beforeEach, afterAll and beforeAll to complete setup and teardown logic. What I would like to do, is to clear up after one particular test. Consider the following:

describe("a family of tests it makes sense to group together", () => {
    ...
    test("something I want to test", () => {
        // some setup needed for just this test
        global.foo = "bar"
        
        // the test
        expect(myTest()).toBe(true)

        // clear up
        delete global.foo
    }
    ...
}

上述问题...

如果以上测试由于某种原因失败,则永远不会运行delete global.foo.这意味着可能所有随后的测试都将失败.我没有看到一个测试失败,而是看到整个测试失败,这可能会造成混淆.

The problem with the above...

If the test above fails for some reason, then delete global.foo is never run. This means that potentially all of the tests following it will fail. Rather than seeing 1 test fail, I see a whole load of tests fail, which could be confusing.

一种解决方案是将delete global.foo添加到我的afterEach中.确实不需要在每次测试后都运行它,但是它也没有任何危害.另一种解决方案是单独进行特定测试,以便afterEach仅适用于该测试.但这似乎也不理想-如果该测试属于其他测试,则可能会与其他测试保持在一起.

One solution is just to add delete global.foo into my afterEach. It doesn't really need to be run after every test, but it doesn't do any harm either. Another solution would be to put the particular test by itself so that afterEach would only apply to it. But this doesn't seem ideal either - if that test belongs with other tests, it aught to be possible for it to remain with them.

是否有一种方法可以仅针对特定测试运行拆卸逻辑(无需在实际测试中运行).在我的特定用例中,第一个概述的解决方案很好,但是我可以想象可能存在需要更细粒度控制的情况.例如,如果我的拆卸方法花了很长时间,我就不想重复很多,因为这会减慢整个测试套件的速度.

Is there a way to run teardown logic for just a specific test (without running it inside the actual test). In my particular use-case the first outlined solution is fine, but I can imagine there might be situations where finer grained control is needed. If my teardown method took a long time for example I wouldn't want to repeat it lots, as this would slow down the whole test-suite.

推荐答案

在许多情况下,测试可以共享一个常见的afterEach清理,即使其中之一是必需的,只要它不影响其他清理即可.

In many cases tests can share a common afterEach clean-up even if it's needed for one of them, as long as it doesn't affect others.

否则,这就是块结构所负责的.可以将一个或多个测试与嵌套的describe分组,以拥有自己的afterEach等块,唯一的缺点是它使报告不太美观:

Otherwise, this is what block structure is responsible for. One or several tests can be grouped with nested describe just to have their own afterEach, etc blocks, and the only downside is that it makes the report less pretty:

describe("a family of tests it makes sense to group together", () => {
    ...
    describe("something I want to test", () => {
        beforeEach(() => {
            global.foo = "bar"
        });
   
        test("something I want to test", () => {
            expect(myTest()).toBe(true)
        }

        afterEach(() => {    
            delete global.foo
        });
    });

beforeEachafterEach可以还原为try..finally:

test("something I want to test", () => {
    try {
        global.foo = "bar"
        
        expect(myTest()).toBe(true)
    } finally {
        delete global.foo
    }
})

这还允许进行异步测试,但要求它们使用async而不是done编写.

This also allows for asynchronous tests but requires them to be written with async instead of done.

这篇关于开玩笑:(仅)单个测试后如何拆卸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆