如何在mocha中增加单个测试用例的超时

Mah*_*a S 390 mocha.js

我在测试用例中提交了一个网络请求,但这有时需要超过2秒(默认超时).

如何增加单个测试用例的超时?

Dan*_*ohn 644

你去:http://mochajs.org/#test-level

it('accesses the network', function(done){
  this.timeout(500);
  [Put network code here, with done() in the callback]
})
Run Code Online (Sandbox Code Playgroud)

对于箭头功能使用如下:

it('accesses the network', (done) => {
  [Put network code here, with done() in the callback]
}).timeout(500);
Run Code Online (Sandbox Code Playgroud)

  • 我正在使用es6箭头函数,不得不回到旧的"函数"定义"this"才能工作. (45认同)
  • timeout以毫秒为单位,默认为2000. (22认同)
  • 有没有办法让它与箭头功能一起工作?编辑:将`.timeout(500)`添加到`it(...).timeout(500)`的结尾 (10认同)
  • Mocha 特别不鼓励箭头函数:https://mochajs.org/#arrow-functions。IMO 值得在答案中指出。 (3认同)
  • @AH箭头功能不起作用的原因是由于[lexical this](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Lexical_this) (2认同)

小智 131

如果您想使用es6箭头功能,可以.timeout(ms)it定义的末尾添加:

it('should not timeout', (done) => {
    doLongThing().then(() => {
        done();
    });
}).timeout(5000);
Run Code Online (Sandbox Code Playgroud)

至少这适用于Typescript.

  • 可悲的是,这只适用于`it`,它不适用于`describe`. (3认同)
  • 有没有办法为`describe()`或`context()`做这个? (3认同)
  • 这有效,但`.timeout`不包含在mocha的DefinitelyTyped类型中:http://i.imgur.com/jQbWCn1.png - 使用`this.timeout(2000)`或`this.slow(500)`使用常规旧函数工作和编译没有错误 (2认同)

chr*_*lly 70

(自从我今天遇到这个)

使用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();
});
Run Code Online (Sandbox Code Playgroud)

编辑:为什么失败:

作为@atoth提到的意见,胖箭头函数没有自己的这一具有约束力.因此,它不可能为的功能结合到这种回调并提供超时功能.

结论:不要对需要增加超时的函数使用箭头函数.

  • 因为箭头功能完全不具备此功能.点击此处了解更多信息:https://blog.getify.com/arrow-this/ (2认同)
  • 是的,但我在答案中解释了这一点.看我的评论.//在代码中.我应该在代码块之外解释它以使其更清晰.这确实存在,但它来自外部范围. (2认同)
  • 我会说这就是为什么你应该只在需要时使用粗箭头,但我已经忘记了“this”是什么。 (2认同)

Sum*_*lly 41

如果您在NodeJS中使用,则可以在package.json中设置超时

"test": "mocha --timeout 10000"
Run Code Online (Sandbox Code Playgroud)

然后你可以使用npm运行:

npm test
Run Code Online (Sandbox Code Playgroud)

  • 这适用于所有测试用例,而不是单个测试用例 (2认同)

and*_*rey 20

从命令行:

mocha -t 100000 test.js
Run Code Online (Sandbox Code Playgroud)

  • 这会增加所有测试用例*的超时*,而不是像问题那样"针对特定的测试用例". (14认同)

Dav*_*her 16

您可能还会考虑采用不同的方法,并使用存根或模拟对象替换对网络资源的调用.使用Sinon,您可以将应用程序与网络服务分离,重点关注您的开发工作.

  • 它不完全*无关紧要; 通常,存储网络响应是有意义的,因此您不依赖于该计算机正在启动或返回正确的响应.但是,如果您正在测试响应本身,那么,您仍然需要这样做. (7认同)
  • 我正在使用sinon/mocha来构建一些集成测试,因此更高的超时是相关的. (2认同)

ald*_*tis 8

对于测试分区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);
            });
    });
});
Run Code Online (Sandbox Code Playgroud)

在该示例中,测试时间为4000(4s).

注意:在测试时间内调用setTimeout(done, 3500)是次要的,done但是clearTimeout(timeOut)避免使用所有这些时间.


小智 8

这对我有用!找不到任何东西使它与 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 
});
Run Code Online (Sandbox Code Playgroud)