使用 NodeJS 和 Mocha 进行测试。我想我了解 before() 和 beforeEach() 的工作原理。问题是,我想添加一个在每个“描述”之前而不是在每个“它”之前运行的设置脚本。
如果我使用 before()
它只会在整个套件中运行一次,如果我使用 beforeEach()
它会在每次测试之前执行,所以我正在努力寻找中间立场。
所以,如果这是我的测试文件:
require('./setupStuff');
describe('Suite one', function(){
it('S1 Test one', function(done){
...
});
it('S1 Test two', function(done){
...
});
});
describe('Suite two', function(){
it('S2 Test one', function(done){
...
});
});
我想让“setupStuff”包含一个在“套件一”和“套件二”之前运行的函数
或者,换句话说,在“S1 测试一”和“S2 测试一”之前,但不在“S1 测试二”之前。
可以吗?
最佳答案
没有类似于 beforeEach
或 before
的调用可以满足您的需求。但它不是必需的,因为您可以这样做:
function makeSuite(name, tests) {
describe(name, function () {
before(function () {
console.log("shared before");
});
tests();
after(function () {
console.log("shared after");
});
});
}
makeSuite('Suite one', function(){
it('S1 Test one', function(done){
done();
});
it('S1 Test two', function(done){
done();
});
});
makeSuite('Suite two', function(){
it('S2 Test one', function(done){
done();
});
});
关于javascript - 在每个套件之前而不是在每个测试之前运行 Mocha 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26107027/