How to write sinon tests where third part api's are concerned

Viewed 1458

I am attempting to test geocode.js which has only one function :

// geocode.js

const googleApiClient = require('@google/maps').createClient({
  key: CONFIG.googleApis.key
});

const poll = zipcode => {

  return new Promise((resolve, reject) => {

     googleApiClient.geocode({
        address: zipcode
      }, function(err, response) {
           resolve(response.json.results); // This is line 22
     });

   });

};

module.exports = {poll}

so i have setup my mocha environment and installed sinon. I can't figure out how to stub the googleApiClient functionality.

I don't actually want to make any kind of external call during the test.

 //geocode.spec.js
describe('geocode', function(){
   before(function(){

     //HOW DO I STUB googleApiClient ?
     sinon.stub ...

   })

});
2 Answers
Related