摩卡,使用 this.skip() 动态跳过测试不起作用



如果条件在"it"中使用this.skip()返回true,我将尝试跳过测试,但我收到一个错误"this.skip不是函数"。这是我试图检查的简单代码:

var async = require('async');
var should = require('chai').should();
var chai = require('chai'),
should = chai.should();
expect = chai.expect;
chai.use(require('chai-sorted'));
describe('Testing skip...n', function() {
this.timeout(1000);
it('test1', (done) => {
this.skip();
console.log("1")
done();
});
it('test2', (done) => {         
console.log("2");
done();
}); 
});

我安装了"mocha@5.2.0"因为我看到它只适用于"摩卡v3.0.0"之后,但我仍然无法让它发挥作用,过去关于这个问题的讨论似乎都无法解决我的问题。

为了在mocha中使用this,请不要使用箭头函数。所以,在你的代码中,你需要把它写成

it('test1', function(done) { // use regular function here
this.skip();
console.log("1")
done();
});

Mocha的最佳实践是阻止箭头功能,如https://mochajs.org/#arrow-功能

希望它能帮助

最新更新