divramod
divramod

Reputation: 1504

hapijs testing with server.inject error catched

i am trying to test hapijs with server.inject

/// <reference path="../../typings/index.d.ts" />
import * as chai from "chai";
let assert = chai.assert;

import server from "../../src/server";
import UserController from '../../src/controllers/userController';
import UserRepository from '../../src/libs/repository/mongo/userRepository';
import {IUser, IUserActivation, IUserCreate} from "../../src/libs/repository/interfaces";

describe("routes/user", function() {

  const userController = new UserController(server, new UserRepository());

  // ========================== [ ACTIVATE ] ==========================
  it.only("/activate: should activate a user", function(done) {
    let user: IUserActivation = {
      '_id': '1234566737465',
      'token': '123234523542345'
    };

    let url = '/api/users/' + user._id + '/' + user.token;
    const request = {
      method: 'PUT',
      url: url,
      payload: user
    };

    server.inject(request).then((response) => {
      let res = JSON.parse(response.payload);
      //assert.strictEqual(res.success, true, '/users/{id}/{token}')
      chai.expect(res.success).to.deep.equal(false);
      chai.expect(res.success).to.deep.equal(true);
      done();
    }).catch((error) => {
      console.log(error.message);
    });
  });
});

The response.success attribute is true. So normally the test should fail because of chai.expect(res.success).to.deep.equal(false);.

But the test fails with the message: Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

When removing the catch clause it also fails with the timeout error.

If I add done() to the end of the catch-clause the test is passing. That is wrong behavior, because the test should fail.

What can i do to get the expected behavior? thanks in advance.

Upvotes: 1

Views: 625

Answers (1)

spgrasso
spgrasso

Reputation: 41

The problem would be that server.inject returns a promise. As long as your using a recent version of Mocha, it can handle that but you don't need to worry about calling done() and rather return the server.inject.

describe("routes/user", function() {

    const userController = new UserController(server, new UserRepository());

    // ========================== [ ACTIVATE ] ==========================
    it.only("/activate: should activate a user", function() { //No done needed
        let user: IUserActivation = {
            '_id': '1234566737465',
            'token': '123234523542345'
        };

        let url = '/api/users/' + user._id + '/' + user.token;
        const request = {
            method: 'PUT',
            url: url,
            payload: user
        };

        //Return the promise
        return server.inject(request).then((response) => {
            let res = JSON.parse(response.payload);
            //assert.strictEqual(res.success, true, '/users/{id}/{token}')
            chai.expect(res.success).to.deep.equal(false);
            chai.expect(res.success).to.deep.equal(true);
        }); //No catch needed.
    });
});

Upvotes: 1

Related Questions