question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

googleapis module issue with mocking single method

See original GitHub issue

I’m using googleapis module which has so many apis and methods. I tried all the possible below thing, nothing working. Why the common approach for mocking is differentiating by modules.

My production code:

let google = require('googleapis');
const iam = google.iam('v1');

I want to mock iam method of googleapis module

Try 1:

jest.unmock('googleapis');
let google = require('googleapis');
google.iam = jest.fn().mockImplementation(function() {
  return { mock: 'mock' };
} );

Try 2:

jest.mock('googleapis', () => {
return jest.fn(() => { iam: 'mock' });
})

This allows mocking, but its working only if my production code is
const iam = google().iam;

Why these 2 ways not working.?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ranyitzcommented, Feb 5, 2018

@popkutty I see. So just do the following and you’re settled 😉

jest.mock('googleapis', () => {
    return { iam: () => 'mock' };
});
0reactions
github-actions[bot]commented, May 13, 2021

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

jest.mock does not mock an ES module without Babel #10025
mock works when the mocked module is a node_modules package that exports CommonJS, but it isn't working for me mocking an ES module...
Read more >
How to mock “GoogleAuth”? - Chetan Patil - Medium
How to mock “GoogleAuth”? ; Libraries used here: ; Step 1: in your test file, import above file and override its internal dependencies...
Read more >
The only 3 steps you need to mock an API call in Jest
The existing tests used all sorts of mocking methods such as jest. ... Import the module you want to mock into your test...
Read more >
Jest mocking google-cloud/storage typescript - Stack Overflow
So jest.mock() esp the @google-cloud/storage modules, needs to be mocked in a different way. The Bucket of the Storage has all the details ......
Read more >
Spies and mocking with Jest - Node.js Developer - Željko Šević
Another way to mock it is by using mockImplementation and providing a function as an ... External modules can be mocked similarly as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found