1

I have dh.js

const checkDExistsCallback = (err, dResp) => {
  if (err)    
    cbResp.error('failed');    

  if (dResp.length > 0) 
    checkDCollectionExists();  
  else 
    cbResp.error('Not found.');  
};
    
const checkDCollectionExists = () => 
{  
  let query = `select sid from tablename where sid = '${objRequestData.dName}' limit 1;`;
  genericQueryCall(query, checkDCollCallback);
}

module.exports = {checkDExistsCallback , checkDCollectionExists }

In my dh.test.ts

const dhExport = require("./DensityHookReceive");
dhExport.checkDCollectionExists = jest.fn().mockImplementation(() => {});

test('check req dh is exists', () => {
  dhExport.checkDExistsCallback(false, '[{}]');
  expect(dhExport.checkDCollectionExists).toBeCalled(); 
});

In dh.js checkDExistsCallback function is invoked the checkDCollectionExists after satisfied the 'if' condition. When you look into the dh.test.ts file I mocked the checkDCollectionExists function in the beginning, but while running the test it did not invoke the mocked function it invokes the actual function. Can you help me to figure it out?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

1 Answers1

1

A function that is used in the same module it was defined cannot be mocked, unless it's consistently used as a method on an object that could be mocked, e.g.

  if (dResp.length > 0) 
    module.exports.checkDCollectionExists();  

instead of

  if (dResp.length > 0) 
    checkDCollectionExists();  

checkDCollectionExists needs to be either moved to another module, or two functions need to be tested as a single unit. It's database call that needs to be mocked.

Estus Flask
  • 206,104
  • 70
  • 425
  • 565
  • Thanks, so do I need to remove the function from "module.exports = {checkDExistsCallback , checkDCollectionExists } " after declared this module.exports.checkDCollectionExists(); – Chandrasekar Kesavan Aug 10 '20 at 08:59
  • 1
    You need to have `module.exports = {checkDExistsCallback` in order for it to be available as `module.exports.checkDCollectionExists` and be mockable as `dhExport.checkDCollectionExists`. Notice that this is a workaround, not an accepted way to do this. And it's not suitable for ES modules. – Estus Flask Aug 10 '20 at 09:09
  • Do you have any suggestion to change in proper way? – Chandrasekar Kesavan Aug 10 '20 at 09:15
  • I thought of changing the function(checkDExistsCallback ) with another parameter like the next callback(checkDCollectionExists) so that it will be easy to mock in all places. Is that way seems good at your point? – Chandrasekar Kesavan Aug 10 '20 at 09:17
  • If the testability of this function is a concern then move it to another module, this way it can be normally mocked with jest.mock. Testability is one of factors that determine how the code should be split between modules. In this specific case I don't see reasons for this because checkDCollectionExists is simple and doesn't add complexity to the test. Mock genericQueryCall instead, it's in another module so can be mocked, isn't it? – Estus Flask Aug 10 '20 at 09:30
  • No, it's on the same module as of now. I need to refactor as you have mentioned. Thanks. – Chandrasekar Kesavan Aug 10 '20 at 09:43