javascript - Function not getting called in Mocha test -


i working on express.js app. current feature creating appointment post request , getting , saving data third party api, sending updated api data in subsequent request. feature working in test, function api data not getting called.

route create appointment:

app.post('/schedule', requesthandler.postschedule); 

the request handler creating appointment:

requesthandler.postschedule = function (req, res) {   let appointment = {     // posted data   };    new appointment(appointment)     .save()     .then(newappointment => {       if(req.body.cityname && req.body.cityname !== '') {         console.log('req.body.cityname', req.body.cityname);         weatherhelper.addnewcityweatherdata(req.body.cityname);       }       return newappointment;     })     .then(newappointment => {       // other stuff       res.send(newappointment);     })     .catch(err => {       error(err);     }); }; 

function add weather data:

exports.addnewcityweatherdata = (city) => {   console.log('city in addnewcityweatherdata', city);   getcurrenttrackingcities(cities => {     if(cities.indexof(city) < 0) {       console.log(city + ' data not in weather');       getweatherdata(city, data => {         console.log('got weather data');         addweatherdatatodb(city, data);       });     } else {       console.log('city exists');     }   }); }; 

function weather data api:

getweatherdata = (city, callback) => {   console.log('getweatherdata called', city);   let url = `http://api.apixu.com/v1/forecast.json?key=${weatherapikey}&q=${city}&days=${10}`   request(url, (err, res, body) => {     console.log('weather data received body');     callback(body);   }); }; 

when testing, feature fails , console logs printed except 'weather data received body' , logs in consequent functions.

here test:

describe.only('weather data', function() {   let requestwithsession = request.defaults({jar: true});    let hashedpass = bcrypt.hashsync('testpass', null);    beforeeach((done) => {     new user({       'name': 'test user',       'email': 'testuser@test.com',       'password': hashedpass     })     .save()     .then(() => {       let options = {         'method': 'post',         'uri': testhost + '/login',         'form': {           'email': 'testuser@test.com',           'password': 'testpass'         }       };       requestwithsession(options, (err, res, body) => {         done();       });     });   }); // beforeeach    aftereach((done) => {     // remove test stuff db   }); // aftereach    it('adds weather data when appointment new city posted', (done) => {     let options = {       'method': 'post',       'uri': testhost + '/schedule',       'form': {         'title': 'test title',         'description': 'test description',         'start_date_time': '2017-07-19 01:00',         'end_date_time': '2017-07-19 02:00',         'cityname': 'new york',         'istrackingweather': 'true'       }     };      // post request add appointment data     requestwithsession(options, (err, res, body) => {       if(err) {         console.log('databaseerror in weather data');         throw {           type: 'databaseerror',           message: 'failed create test setup data'         };       }        let options = {         'method': 'get',         'uri': testhost + '/allweather'       };        // subsequesnt request updated weather data       requestwithsession(options, (err, res, body) => {         let found = false;         weatherdata = json.parse(body);         // console.log('weatherdata in test', weatherdata);         weatherdata.foreach(weather => {           if(weather.location && weather.location.name === 'new york') {             found = true;           }         });         expect(found).to.be.true;         done();       });      });   }); }); // weather data 

here terminal output:

mocha test

can please tell me doing wrong?

when run test test suite make request test server, , code handles request in test server makes another request host.

you not see 'weather data received body' because request handled test server not waiting request test server makes. addnewcityweatherdata has no callback , not return promise, code calls goes on merry way without waiting complete. should modify allow calling code wait result.

also, i'm not seeing how data request initiated test server folded request comes test suite. may have add code too, unless addweatherdatatodb(city, data); taking care of automatically somehow.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -