-1

Is using Promise a better solution instead of using timeout for API testing using mocha/chai? I am getting error like this below for lot of tests and want to prevent those errors. Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

describe('Email Tests', function() {
            let messagingApiPath = '/v2/email';
            let testData = require(`../../${testJsonFileName}`);

            let positiveAssertions = function(response) {
                console.log('Response : \n', response.text);
                expect(response.statusCode).equals(200);
                expect(response.status).equals(200);
                expect(response.emailReferenceId == 36);
            };

            describe('POST /v2/email with TO and CC', function() {

                console.log('Test Data File: ' + testJsonFileName);
                describe('with To:  CC:  Test-Case-1', function() {
                    it('response with email id reference expected', function(done) {

                        request
                            .post(messagingApiPath)
                            .send(input)
                            .expect((response) => positiveAssertions(response))
                            .end(done);
                    });
                });
          //many more tests like  describe('POST /v2/email')

      });
 });
RajKon
  • 420
  • 1
  • 8
  • 20
  • 1
    If it’s actually just taking a long time, promises aren’t going to prevent timeouts. If you’re forgetting to call the `done` callback, you might find promises easier to work with, sure. – Ry- May 19 '18 at 00:45
  • 1
    Poste some part of the code, this do not bring any light to you problem. – desoares May 19 '18 at 00:50
  • 1
    We can't really advise you much without seeing relevant code. Properly written code works with any tools you use. Promises are a more modern way to handle async code, but they don't "solve" timeout problems magically by themselves. They're just a tool that has to be used properly. – jfriend00 May 19 '18 at 00:58

1 Answers1

0

Thanks for the comments that helped me get to an answer. For now (since I do not want to mock the response) editing the grunt configuration (increasing the time out) helped.

 // Grunt initial configuration
grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    mochaTest: {
        unit: {
            options: {reporter: 'spec'},
            src: ['test/lib/setup.js', 'core/test/**test.js']
        },
        functional: {
            options: { reporter: 'spec', timeout: 40000 },
            src: ['test/functional/setup.js','test/functional/v1/mytests.js']
        }
    },
    //more code that is removed for simplicity
});
RajKon
  • 420
  • 1
  • 8
  • 20