Google-api-nodejs-client: Can't stub/mock library's functions

Created on 19 Mar 2015  路  1Comment  路  Source: googleapis/google-api-nodejs-client

I have a function like this

module.exports = function() {
    return new BPromise(function (resolve, reject) {
        analytics.management.accountSummaries.list({}, function (err, results) {
            if (err !== null) {
                reject(new Error(err));
            } else {
                resolve(profileId);
            }
        });
    });
}

and here is the test

const analytics = require('googleapis').analytics({version: 'v3'}),
stub = sinon
            .mock(analytics.management.accountSummaries)
            .expects('list')
            .once()
            .yields(null, dummy);

        analyticsService.getProfileFromTracker('UA-3213-321')
            .then(function () {
                stub.verify();
                done();
            })
            .catch(Error, function (err) {
                stub.verify();
                done();
            });

But the stubbed function will never run. What am I doing wrong.

Thanx guys

triage me

Most helpful comment

Greetings! I know this issue has been around for a while, but with the latest versions of sinon and googleapis, this works great!

const {google} = require('googleapis');
const sinon = require('sinon');

const analytics = google.analytics('v3');
const stub = sinon
  .mock(analytics.management.accountSummaries)
  .expects('list')
  .once()
  .resolves({});

analytics.management.accountSummaries.list()
  .then(r => console.log(r))
  .catch(e => console.error(e));

Please do let us know if you have any other questions :)

>All comments

Greetings! I know this issue has been around for a while, but with the latest versions of sinon and googleapis, this works great!

const {google} = require('googleapis');
const sinon = require('sinon');

const analytics = google.analytics('v3');
const stub = sinon
  .mock(analytics.management.accountSummaries)
  .expects('list')
  .once()
  .resolves({});

analytics.management.accountSummaries.list()
  .then(r => console.log(r))
  .catch(e => console.error(e));

Please do let us know if you have any other questions :)

Was this page helpful?
0 / 5 - 0 ratings