Answers:
您可以在这里:http : //mochajs.org/#test-level
it('accesses the network', function(done){
  this.timeout(500);
  [Put network code here, with done() in the callback]
})
对于箭头功能,使用如下:
it('accesses the network', (done) => {
  [Put network code here, with done() in the callback]
}).timeout(500);
before(function(done){this.timeout(5 * 1000);...});
                    .timeout(500)到it(...).timeout(500)
                    如果希望使用es6箭头功能,可以.timeout(ms)在it定义的末尾添加a :
it('should not timeout', (done) => {
    doLongThing().then(() => {
        done();
    });
}).timeout(5000);
至少这在Typescript中有效。
.timeout包含在摩卡(Mocha)的DefinitelyTyped类型中:i.imgur.com/jQbWCn1.png-使用this.timeout(2000)或this.slow(500)与常规旧函数一起工作并可以正确编译
                    it,不适用于describe。
                    describe()还是context()?
                    .timeout现在包含在DefinitelyTyped的Mocha输入中:Mocha.IRunnable。但是,如果您使用Webstorm IDE来运行这些测试,请注意以下几点:由于任何原因,WebStorm的Mocha集成插件仍然无法识别.timeout()附加了Mocha测试(这意味着它们旁边没有“运行”按钮),因此,我主张避免使用箭头功能以允许使用this.timeout()代替功能。
                    (因为我今天遇到了这个)
使用ES2015粗箭头语法时要小心:
这将失败:
it('accesses the network', done => {
  this.timeout(500); // will not work
  // *this* binding refers to parent function scope in fat arrow functions!
  // i.e. the *this* object of the describe function
  done();
});
编辑:为什么失败:
作为@atoth在评论中提到,胖箭头函数没有自己的这一具有约束力。因此,不可能将it函数绑定到该回调并提供超时功能。
底线:对于需要增加超时的功能,请不要使用箭头功能。
this箭头函数没有绑定-用不同的方式暗示它们具有某种不同。他们只有词汇范围。您不能绑定不存在的内容。这就是为什么.bind,.call等无法使用它的原因。
                    this是什么。
                    如果在NodeJS中使用,则可以在package.json中设置超时
"test": "mocha --timeout 10000"
那么您可以使用npm像这样运行:
npm test
用于以下方面的测试导航Express:
const request = require('supertest');
const server = require('../bin/www');
describe('navegation', () => {
    it('login page', function(done) {
        this.timeout(4000);
        const timeOut = setTimeout(done, 3500);
        request(server)
            .get('/login')
            .expect(200)
            .then(res => {
                res.text.should.include('Login');
                clearTimeout(timeOut);
                done();
            })
            .catch(err => {
                console.log(this.test.fullTitle(), err);
                clearTimeout(timeOut);
                done(err);
            });
    });
});
在示例中,测试时间为4000(4s)。
注意:在测试时间内setTimeout(done, 3500)小于done被调用的时间,但是clearTimeout(timeOut)在所有这些时间内都避免使用过。
这对我有用!找不到任何可以与before()一起使用的东西
describe("When in a long running test", () => {
  it("Should not time out with 2000ms", async () => {
    let service = new SomeService();
    let result = await service.callToLongRunningProcess();
    expect(result).to.be.true;
  }).timeout(10000); // Custom Timeout 
});