javascript - 笑话:如何在(仅)一个单独的测试后拆解
问题描述
jest
提供afterEach
、beforeEach
和来完成设置afterAll
和beforeAll
拆卸逻辑。我想做的是在一项特定测试后进行清理。考虑以下:
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
仅适用于它。但这似乎也不理想——如果该测试属于其他测试,那么它可能会保留在它们身上。
我的问题:
有没有办法只为特定测试运行拆解逻辑(而不在实际测试中运行它)。在我的特定用例中,第一个概述的解决方案很好,但我可以想象可能存在需要更细粒度控制的情况。例如,如果我的拆卸方法需要很长时间,我不想重复很多,因为这会减慢整个测试套件的速度。
解决方案
在许多情况下,测试可以共享一个公共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
.
推荐阅读
- azure-machine-learning-studio - 推荐器拆分返回空数据集
- python - 将一堆不同的数据聚合到具有多个列的单个 groupby 中
- c# - C# EventHandler 捕获对 Console.WriteLine() 函数的调用
- haskell - Haskell中的长度vs头部vs最后
- date - 如何将月份和年份的字符串字段转换为 hive 中的日期格式(使用 SAS)?
- java - 如何在java中读取rar文件注释?
- python - Python亵渎检查器错误
- python-2.7 - 如何使用 gnuplot-py 制作增强的字体和粗线?
- sql - 查询以在特定条件下通过重置递增计数
- c++ - std::sort 使用对指针和值执行的自定义函数