最简单的解决方案,我看到的是覆盖全局的功能describe
和it
使他们接受第三个可选参数,它必须是一个布尔值或返回一个布尔函数 - 告诉电流是否套房/规格应该执行。当重写时,我们应该检查这个第三个可选参数是否解析为true
,如果是,那么我们称之为xdescribe
/xit
(或ddescribe
/iit
取决于Jasmine版本),这是Jasmine的方法来跳过suite/spec,而不是原始describe
/it
。该块必须在测试之前执行,但Jasmine包含在页面之后。在Karma中,只需将这些代码移动到一个文件中,并将其包含在karma.conf.js
的测试文件之前。下面是代码:
(function (global) {
// save references to original methods
var _super = {
describe: global.describe,
it: global.it
};
// override, take third optional "disable"
global.describe = function (name, fn, disable) {
var disabled = disable;
if (typeof disable === 'function') {
disabled = disable();
}
// if should be disabled - call "xdescribe" (or "ddescribe")
if (disable) {
return global.xdescribe.apply(this, arguments);
}
// otherwise call original "describe"
return _super.describe.apply(this, arguments);
};
// override, take third optional "disable"
global.it = function (name, fn, disable) {
var disabled = disable;
if (typeof disable === 'function') {
disabled = disable();
}
// if should be disabled - call "xit" (or "iit")
if (disable) {
return global.xit.apply(this, arguments);
}
// otherwise call original "it"
return _super.it.apply(this, arguments);
};
}(window));
和使用例如:
describe('foo', function() {
it('should foo 1 ', function() {
expect(true).toBe(true);
});
it('should foo 2', function() {
expect(true).toBe(true);
});
}, true); // disable suite
describe('bar', function() {
it('should bar 1 ', function() {
expect(true).toBe(true);
});
it('should bar 2', function() {
expect(true).toBe(true);
}, function() {
return true; // disable spec
});
});
See a working example here
我也偶然发现this issue其中的想法是加入链方法.when()
为describe
和it
,这与我上面描述的几乎相同。它看起来更好,但实施起来有点困难。
describe('foo', function() {
it('bar', function() {
// ...
}).when(anything);
}).when(something);
如果你是第二种方法很感兴趣,我会很乐意与它一点点玩,尝试实施连锁.when()
。
更新:
茉莉花使用第三个参数为超时选项(see docs),所以我的代码示例替换该功能,这是不正常。我喜欢@milanlempera和@MarcoCI更好的回答,我的似乎有点hacky和不直观。我会尝试尽快更新我的解决方案,不要打破Jasmine默认功能的兼容性。
太好了。我用它来禁止fdescribe并适合构建env。只需重写fdescribe和fit并抛出异常。 –