Mocha,使用this.skip()动态跳过测试无法正常工作

ron*_*one 4 testing skip mocha.js

我试图跳过测试,如果条件this.skip()在“ it”中使用true返回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();
        }); 

});
Run Code Online (Sandbox Code Playgroud)

我安装了“ mocha@5.2.0”,因为我看到它仅在“ mocha v3.0.0”之后才能运行,但是我仍然无法使它正常工作,并且过去关于该主题的讨论都没有解决我的问题。

dee*_*wan 11

为了this在摩卡咖啡中使用,请勿使用箭头功能。因此,在您的代码中,您需要将其编写为

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

Mocha的最佳实践是不鼓励使用箭头功能,如https://mochajs.org/#arrow-functions中所述

希望能帮助到你