首页 > 解决方案 > 笑话:如何在(仅)一个单独的测试后拆解

问题描述

jest提供afterEachbeforeEach和来完成设置afterAllbeforeAll拆卸逻辑。我想做的是在一项特定测试后进行清理。考虑以下:

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永远不会运行。这意味着它之后的所有测试都可能失败。我没有看到 1 个测试失败,而是看到一大堆测试失败,这可能会令人困惑。

潜在(非理想)解决方案

一种解决方案就是添加delete global.foo到我的afterEach. 它实际上并不需要在每次测试后运行,但它也不会造成任何伤害。另一种解决方案是将特定测试单独放置,以便afterEach仅适用于它。但这似乎也不理想——如果该测试属于其他测试,那么它可能会保留在它们身上。

我的问题:

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

标签: javascripttestingjestjsteardown

解决方案


在许多情况下,测试可以共享一个公共afterEach清理,即使其中一个需要它,只要它不影响其他人。

否则,这是块结构负责的。可以将一个或多个测试与嵌套组合在一起describe,以拥有自己的afterEach, etc 块,唯一的缺点是它使报告不那么漂亮:

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
        });
    });

beforeEach并且afterEach可以脱糖为try..finally

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

这也允许异步测试,但需要使用async而不是done.


推荐阅读