node.js - Node function works but is undefined when run via Mocha -


i've set working function using twilio's api. function works when enter parameters through ui, when run test in mocha fails stating function undefined. have 1 other test in mocha runs before test , first test passes.

twilioclient.js:

var config = require('./config'); var client = require('twilio')(config.accountsid, config.authtoken);  sendsms = function(to, message) {   client.messages.create({     body: message,     to: to,     from: config.sendingnumber     // mediaurl: 'http://www.yourserver.com/someimage.png'   }, function(err, data) {     if (err) {       console.error('could not notify administrator');       console.error(err);       return 'could not notify administrator';     } else {       console.log('administrator notified');       return 'administrator notified';     }   }); };  module.exports.sendsms = sendsms; 

my indexspec.js file:

var chai= require('chai'); var expect = require('chai').expect; var twilionotifications = require('./send text/js/twilionotifications'); var cfg = require('./send text/js/config.js'); var twilio = require('twilio'); var tclient = require('./send text/js/twilioclient.js');  describe('twiliovars', function() {     it('returns true if variables not null nor undefined', function() {         var tvars = cfg.twiliovars(cfg.reqconfig);          expect(tvars).to.equal(true);     }) })  describe('msgsent', function() {     it('confirms if message can sent', function() {         var receiver = <my number>;         var message = 'my message';         var sentmsg = tclient.sendsms(receiver, message);             expect(sentmsg).to.equal('administrator notified');     }) }) 

my config.js file:

var cfg = {}; cfg.accountsid = <some string>; cfg.authtoken = <some string>; cfg.sendingnumber = <some string>;  var requiredconfig = [cfg.accountsid, cfg.authtoken, cfg.sendingnumber];  // testing: cfg.reqconfig = requiredconfig; cfg.twiliovars = function(arr) {     if (arr[0] && arr[1] && arr[2]) {         return true;     }     return false; }; // end testing  var isconfigured = requiredconfig.every(function(configvalue) {   return configvalue || false; });  if (!isconfigured) {   var errormessage =     'twilio_account_sid, twilio_auth_token, , twilio_number must set.';    throw new error(errormessage); }  // export configuration object module.exports = cfg; 

any insight why function runs why test fails appreciated.

p.s. error below:

 1) msgsent confirms if message can sent:  assertionerror: expected undefined equal 'administrator notified'   @ context.<anonymous> (c:\users\james bradley\code_tests\twilio\twilio_test\indexspec.js:22:22)   @ callfn (c:\users\james bradley\appdata\roaming\npm\node_modules\mocha\lib\runnable.js:326:21)   @ test.runnable.run (c:\users\james bradley\appdata\roaming\npm\node_modules\mocha\lib\runnable.js:319:7)   @ runner.runtest (c:\users\james bradley\appdata\roaming\npm\node_modules\mocha\lib\runner.js:422:10)   @ c:\users\james bradley\appdata\roaming\npm\node_modules\mocha\lib\runner.js:528:12   @ next (c:\users\james bradley\appdata\roaming\npm\node_modules\mocha\lib\runner.js:342:14)   @ c:\users\james bradley\appdata\roaming\npm\node_modules\mocha\lib\runner.js:352:7   @ next (c:\users\james bradley\appdata\roaming\npm\node_modules\mocha\lib\runner.js:284:14)   @ immediate._onimmediate (c:\users\james bradley\appdata\roaming\npm\node_modules\mocha\lib\runner.js:320:5) 

your sendsms function asynchronous, need wait complete before testing it's output. consider adding callback function can invoke when client.messages.create() makes callback, , employ done. or consider returning promise sendsms.

for example:

twilioclient.js:

... sendsms = function(to, message, cb) {   client.messages.create({     ...   }, function(err, data) {     ...     cb(err, data);   }); }; 

indexspec.js:

... describe('msgsent', function() {   it('confirms if message can sent', function(done) {     ...     var sentmsg = tclient.sendsms(receiver, message, function(err, data) {       expect(data.sentmsg).to.equal('administrator notified');       done();     });   }); }); 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

c# - Json.Net Serialize String from URI -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -