Вы можете смоделировать весь модуль googleapis
с помощью mock-require
.
const mock = require('mock-require');
mock('googleapis', {
google: {
calendar: () => ({
calendarList: {
list: () => {
return Promise.resolve({
data: {
foo: 'bar'
}
});
}
}
})
}
});
После того, как вы смоделировали его, ваш модуль будет использовать смоделированный модуль вместо оригинала, так что выможете проверить это. Поэтому, если ваш модуль предоставляет метод, который вызывает API, что-то вроде этого:
exports.init = async () => {
const { google } = require('googleapis');
const googleCalendar = google.calendar('v3');
let { data } = await googleCalendar.calendarList.list({
auth: 'auth'
});
return data;
}
Тест будет
describe('test', () => {
it('should call the api and console the output', async () => {
const result = await init();
assert.isTrue(result.foo === 'bar');
});
});
Вот небольшой репозиторий, с которым можно поиграть: https://github.com/moshfeu/mock-google-apis