Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocha test runs twice

i'm trying to figure out why this test runs twice:

it ( "should verify file exists with a one name filename ", function ( done ){   
    var fileNameOneChar = "/Users/myusername/t.mp3"; 
    console.log(" the file path ", fileNameOneChar ); 
    expect( myApi.fileExists( fileNameOneChar ) ).to.be( true ); 
    done();
});

the test runs twice in a row, (although its only defined once) the first time it passes (which is okay, that file actually exists and the api is working fine), the second time it fails with an error : "Error: timeout of 5000ms exceeded" .

WHy does the test runs twice ? i have looked everywhere foe the time out error for a while now, havent found anything on Mocha.

like image 604
elcomputerguy Avatar asked Sep 03 '25 03:09

elcomputerguy


1 Answers

The same was happening to me minutes ago and I found a workaround (not a fix): Run the tests in sync mode.

Basically you need to avoid setting up the done parameter of the it method

Below an example of how my test looks like now

describe('API /users Endpoint', function () {
  it('GET /users should return a JSON list of users', function (done) {
    request
      .get(config.api.prefix+'users')
      .set('bearer',config.api.key)
      .end(function(err,res){
        if (err) {
          return done(err);
        }
        res.statusCode.should.equal(200);
        res.body.should.not.be(null);
        res.body.posts.should.not.be(undefined);
        res.body.posts.should.not.have.length(0);
      });
  });

Hope it helps!

like image 182
Franco Laiuppa Avatar answered Sep 07 '25 14:09

Franco Laiuppa