Tuesday, 4 April 2017

Unable to specify the thrown error by the function to be tested in mocha nodejs. expect.to.throw (Error) not working

This is function for which I have written test cases.

exports.sendOTPParser = event => {
console.log('sendOTPParser');
event = event || {};
try {
const body = JSON.parse(event.body);
const phoneNumber = body.phonenumber;
if (typeof phoneNumber !== 'string') {
  throw new Error('MalformedRequest');
}
event.phoneNumber = body.phonenumber;
return event;
} catch (error) {
console.log('Error in isEmailUniqueParser: %s', error.message);
throw new Error('MalformedRequest');
}

}

And this is the mocha code to test the given function. But, it is not catching error. Whats wrong??

var file = require('../src/auth/eventParsers');

var expect    = require('chai').expect;

describe('sendOTPParserTest', function(){

var event1 =  {"body": '{"phonenumber": 1234}'};
var event2 =  {"body": '{"phonenumber": "1234"}'};

// event2.body.phonenumber = '12345';
// event1.body.phonenumber = 12345;

it('returnTest', function(){
var result1 = file.sendOTPParser(event1);
// var result2 = file.sendOTPParser(event2);                expect(result2.phoneNumber).to.equal(JSON.parse(event2.body).phonenumber);
    expect(result1).to.throw(Error);

});

});



via Akshay Barpute

No comments:

Post a Comment