Search code examples
node.jsmocha.jsintegration-testingchaisupertest

NodeJs testing Error: Timeout of 2000ms exceeded


I am testing multiple responses but always end up with the same error message:

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/this/file/path.js)
   let invalid = 'something_invalid';

    it('With valid appid', (done) => {
        request(server).get(`/game/info/${valid}`)
            .then((err, res) => {

                let json = res.body;

                expect(200);
                expect(json.name).to.equal("Rust");
                done();
            }).catch(err => console.log(err))
    });

Where the response is:

{
    "name": "Rust",
    "appid": 252490,
    "description": "The only aim in Rust is to survive - Overcome struggles such as hunger, thirst and cold. Build a fire. Build a shelter. Kill animals. Protect yourself from other players.",
    "publishers": [
        "Facepunch Studios"
    ],
    "price_text": "€33.99",
    "platforms": {
        "windows": true,
        "mac": true,
        "linux": false
    },
    "likes": 374668
}

I've looked everywhere but the solution to the problem never solved it. Any idea on what I am doing wrong?


Solution

  • Try to set a longer timeout when running your test:

    mocha --timeout 10000
    

    Or in each suite or each test manually:

    describe('...', function(){
      this.timeout(10000);
    
      it('...', function(done){
        this.timeout(10000);
        setTimeout(done, 10000);
      });
    });