0

I am using mocha and sinon for test the node services, In controller I have getToken npm module for getting the token with name and value as parameters and in spec file I trying to send empty name as parameter using withargs but the response getting success excepted result is token creating fail please help on this issue.

controller.ts

import {getToken} from './tokenUtil';

export async function userInfo(req:Request,res:Response){
try{
let token = await getToken(name,value);
}
catch(error){
res.send({status:'Failue',message:error});
return
}
res.send({status:'success',message:'token creation success'})
}

tokenUtil.ts

export async function getToken(name,value){

// token code here

}

token.spce.ts

import * as sinon from 'sinon';
import * as proxyquire from 'proxyquire';

describe('get token',()=>{
let req:any;
let res:any;
beforeEach(()=>{
res={
send:sinon.stub();
}

it('getting error when given empty name',async()=>{
let tokenStub = sinon.stub().withArgs('',tokenValue).returns(undefined);

let tokenctl=proxyquire('./controller',{
'./tokenUtil':tokenStub

})
await tokenctl.userInfo(req,res);
sinon.assert.calledWithExactly(res.send,{status:'Failue',message:'token creating fail'})
})
})


})

1 Answers1

0

You are testing the controller.ts module, so the test file name should be controller.spec.ts or controller.test.ts.

Since the ./tokenUtil use named exports, so the tokenStub should be an object.

You should use sinon.stub().rejects() to create a promise stub with rejected value.

E.g.

controller.ts:

import { getToken } from './tokenUtil';
import { Request, Response } from 'express';

export async function userInfo(req: Request, res: Response) {
  const { name, value } = req.body;
  try {
    let token = await getToken(name, value);
    res.send({ status: 'success', message: 'token creation success' });
  } catch (error) {
    res.send({ status: 'Failue', message: error });
  }
}

tokenUtil.ts:

export async function getToken(name, value) {
  // token code here
}

controller.test.ts:

import sinon from 'sinon';
import proxyquire from 'proxyquire';

describe('get token', () => {
  let req: any;
  let res: any;
  beforeEach(() => {
    res = {
      send: sinon.stub(),
    };
  });

  it('should create token success', async () => {
    req = { body: { value: '123', name: 'teresa teng' } };
    let tokenStub = {
      getToken: sinon.stub().withArgs(req.body.name, req.body.value).resolves(),
    };

    let tokenctl = proxyquire('./controller', {
      './tokenUtil': tokenStub,
    });
    await tokenctl.userInfo(req, res);
    sinon.assert.calledWithExactly(res.send, { status: 'success', message: 'token creation success' });
  });

  it('should handle error when given empty name', async () => {
    const tokenValue = '123';
    req = { body: { value: tokenValue, name: '' } };
    const error = new Error('token creating fail');
    let tokenStub = {
      getToken: sinon.stub().withArgs('', tokenValue).rejects(error),
    };

    let tokenctl = proxyquire('./controller', {
      './tokenUtil': tokenStub,
    });
    await tokenctl.userInfo(req, res);
    sinon.assert.calledWithExactly(res.send, { status: 'Failue', message: error });
  });
});

Test result:

  get token
    ✓ should create token success (101ms)
    ✓ should handle error when given empty name


  2 passing (112ms)
Lin Du
  • 88,126
  • 95
  • 281
  • 483