Search code examples
mocha.jschaichai-http

Mocha - why before() was not finished 'before' it and how can I do that?


I need to call API request in before (post lib.js) and check results in it.

No matter what I'm trying every time is executed it before finishing before and res variable is empty as you can see on the beginning of output - right bellow the millis with hash mark.

test.js

const lib = require('../lib/lib.js');
global.serverurl = process.env.SERVERURL;

describe('Template Scenario', function () {

    global.loginData = require('../data/template.json');

    describe('Login user', function () {

        var res;

        before(function () {
            res = lib.post('/rest/security/login/', {username: loginData.username, password: loginData.password});
        });

        it('should return right status code', function () {
            console.log('###########################'+Date.now());
            console.log(JSON.stringify(res));
            res.should.have.status(200);
        });

lib.js

const chai = require('chai');
chai.use(require('chai-http'));

exports.post = function (api_endpoint, request_body) {
    return chai.request(serverurl)
    .post(encodeURI(api_endpoint))
    .send(request_body)
    .then(function (r) {
        console.log('***************************'+Date.now());
        console.log(JSON.stringify(r));
    });
}

output

  Template Scenario
    Login user
###########################1571734950189
{}
      1) should return right status code


  0 passing (59ms)
  0 pending
  1 failing

  1) Template Scenario
       Login user
         should return right status code:
     AssertionError: expected {} to have keys 'status', or 'statusCode'



***************************1571734950409
{"req":{"method":"post","url":"https:// ....

Solution

  • Solved

    When I changed before to async, and removed then in post function, it works as expected.

    test.js

    const chai = require('chai');
    const should = chai.should();
    const lib = require('../lib/lib.js');
    global.serverurl = process.env.SERVERURL;
    
    var res;
    
    describe('Template Scenario', function () {
    
        global.loginData = require('../data/template.json');
    
        describe('Login user', function () {
    
            before(async function () {
                res = await lib.post('/rest/security/login/', {username: loginData.username, password: loginData.password});
            });
    
            it('should return right status code', function () {
                res.should.have.status(200);
            });
    

    lib.js

    const chai = require('chai');
    chai.use(require('chai-http'));
    
    exports.post = async function (api_endpoint, request_body) {
        return chai.request(serverurl)
            .post(encodeURI(api_endpoint))
            .send(request_body);
    }
    

    output

      Template Scenario
        Login user
          ✓ should return right status code
          ✓ should return correct Json schema
    
    
      2 passing (97ms)