Javascript 如何避免在某个特定it块之前运行beforeEach?
我想在Javascript 如何避免在某个特定it块之前运行beforeEach?,javascript,mocha.js,Javascript,Mocha.js,我想在it块1.5之前阻止beforeach运行。我该怎么做?选项1 我建议使用嵌套描述,例如: describe('1', function () { beforeEach(function () { // do this before each it EXCEPT 1.5 }); it('1.1', function () { }); it('1.2', function () { }); it('1.3', function () { });
it
块1.5
之前阻止beforeach
运行。我该怎么做?选项1
我建议使用嵌套描述,例如:
describe('1', function () {
beforeEach(function () {
// do this before each it EXCEPT 1.5
});
it('1.1', function () {
});
it('1.2', function () {
});
it('1.3', function () {
});
it('1.4', function () {
});
it('1.5', function () {
// beforeEach shouldn't run before this
});
});
“幕后描述”将注册beforeach函数,该函数将被所有ITFunction调用(如果存在)
选择2 it函数将按顺序调用,因此您也可以在每个函数运行之前使用闭包来控制它们的运行时间-但这有点不方便-例如:
describe('1', function () {
describe('1 to 4', function () {
beforeEach(function () {
// do this before each it EXCEPT 1.5
});
it('1.1', function () {
});
it('1.2', function () {
});
it('1.3', function () {
});
it('1.4', function () {
});
});
describe('only 5', function () {
it('1.5', function () {
// beforeEach shouldn't run before this
});
});
除了chriskelly提供的答案之外,你可能会对这条线索有更多的了解
describe('1', function () {
var runBefore = true
beforeEach(function () {
// do this before each it EXCEPT 1.5
if (runBefore) {
// actual code
}
});
// functions removed for brevity
it('1.4', function () {
runBefore = false;
});
it('1.5', function () {
// beforeEach shouldn't run before this
// turn it back on for 1.6
runBefore = true;
});
});