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

Mocha, using this.skip() to skip tests dynamically doesn't work

如果在 "it" 中使用 this.skip(),如果条件 returns 为真,我试图跳过测试,但我收到错误 "this.skip is not a function"。 这是我要检查的简单代码:

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 ",因为我看到它只能在 "mocha v3.0.0" 之后运行, 但我仍然无法让它工作,并且 none 过去关于该主题的讨论似乎解决了我的问题。

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

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

Mocha 中的最佳做法是不鼓励箭头函数,如 https://mochajs.org/#arrow-functions

中所述

希望对您有所帮助