gpt4 book ai didi

javascript - Mocha :How to bail only failed describe() in nested describe()?

转载 作者:行者123 更新时间:2023-11-29 23:43:31 25 4
gpt4 key购买 nike

我的测试结构如下

describe('Test Suite'){
describe('First Test Case'){
it('1st step'){};
it('2nd step'){};
it('3rd step'){};
}
describe('Second Test Case'){
it('1st step'){};
it('2nd step'){};
it('3rd step'){};
}
}

我想使用 --bail 这样如果在第一个测试用例中任何 it() 失败那么 describe() 应该被保释。但是第二个测试用例应该运行。

我通过使用得到了预期的结果:

describe('Test Suite'){
this.bail(false)
describe('First Test Case'){
this.bail(true);
it('1st step'){};
it('2nd step'){};
it('3rd step'){};
}
describe('Second Test Case'){
this.bail(true);
it('1st step'){};
it('2nd step'){};
it('3rd step'){};
}
}

如果不在每个 describe() 中显式地将 bail 分配给 true,是否还有其他方法可以做到这一点?

最佳答案

我找到了解决方法,也许它会有所帮助。

您可以不通过文件而是通过我的函数名称或测试名称来设置您的条件。

在设置文件中:

const FAILED_TESTS = {};
// Skip test if first test from folder failed
beforeEach(function() {
if (FAILED_TESTS[this.currentTest.file]) {
this.skip();
}
});

afterEach(function() {
if (this.currentTest.state === "failed") {
FAILED_TESTS[this.currentTest.file] = true;
}
});

关于javascript - Mocha :How to bail only failed describe() in nested describe()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44646751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com