How to increase timeout for a single test case in mocha

前端 未结 8 1829
走了就别回头了
走了就别回头了 2020-11-28 01:59

I\'m submitting a network request in a test case, but this sometimes takes longer than 2 seconds (the default timeout).

How do I increase the timeout for a single te

相关标签:
8条回答
  • 2020-11-28 03:02

    This worked for me! Couldn't find anything to make it work with 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 
    });
    
    0 讨论(0)
  • 2020-11-28 03:03

    For test navegation on 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);
                });
        });
    });
    

    In the example the test time is 4000 (4s).

    Note: setTimeout(done, 3500) is minor for than done is called within the time of the test but clearTimeout(timeOut) it avoid than used all these time.

    0 讨论(0)
提交回复
热议问题