googleapis module issue with mocking single method
See original GitHub issueI’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:
- Created 6 years ago
- Comments:7 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@popkutty I see. So just do the following and you’re settled 😉
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.