1

i want to do a unit test with async function on the code. and here is my code on user.test.js

'use strict'
const UserDomain = require("../../../../../../bin/modules/users/repositories/commands/domain")
const UserHandler = require("../../../../../../bin/modules/users/repositories/commands/command_handler")
const expect = require('chai').expect;
const assert = require('chai').assert;
const sinon = require('sinon');
describe('User domain', () => {
describe('".login(data)"', () => {
    let user;
    beforeEach( () => {
        user = {
            clientId : "adithyavisnu",
            clientSecret : "secretOfmine@19"
        }

    });
    it("should return error when username/password is empty", (done)=> {
        done();
        // let 
    })

    it("should return object", async () => {
        const domainStub = sinon.stub(UserDomain.prototype, 'login');
        const result = await UserHandler.login(user);
        sinon.assert.calledOnce(domainStub);
        domainStub.restore();
    })
});
});

If the normal code (not the unit test code above) the const result = await UserHandler.login(user); will have an object response, but when i do in user.test.js it do not get the response. the result is undefined.

here are the user_handler code

'use strict';
const User = require('./domain');
const login = async (data) => {
const postData = async () => {
    const user = new User();
    const result = await user.login(data);
    return result;
}
const response = await postData();
return response;
}

Is there something i did wrong on the code or some code is missing?

I am sorry if you do think there is unclear information

Thank you for the responses

Adithya Visnu
  • 63
  • 1
  • 5
  • try out https://www.npmjs.com/package/promise i prefer them instead ov async-await – BraveButter Jun 28 '18 at 09:28
  • Hi @BraveButter , actually it will takes time to refactoring all the code from async-await to `promise modules` due to lack experiences of mine :( . But thank you for your suggest before. Really appreciate it and hopefully I can using that for next iteration – Adithya Visnu Jun 29 '18 at 05:53

1 Answers1

1

In the normal flow, the UserHandler calls the Domain.login method and returns the result object. When you run the unit test you are stubbing the Domain.login method. so, it wont return the result as normal flow. You can either make the stub return some result object and test that or just spy the Domain.login instead of stubbing it , if you just want to just check that the Domain.login was called without altering its behavior. Read more on stubs/spies here if you would like - http://sinonjs.org/releases/v1.17.7/stubs/

Kaushik Srinath
  • 209
  • 1
  • 7
  • Thank your for your answer, really appreciate it. So, we cannot get the real response like normal flow because we are stubbing, and the options to check the response is implementing with declared object structure e.g `.return(responsesObject)` function. That is i got from your answer, is it true? – Adithya Visnu Jun 29 '18 at 05:45
  • yes. you can make the stub return what you want and test. – Kaushik Srinath Jun 29 '18 at 06:01
  • Thank you for your explanation, it makes me know what i going to do – Adithya Visnu Jun 29 '18 at 08:09