Nodejs override function in module

I am trying to test a function in a module. This function (I will call it function_a) calls another function (function_b) in the same file. Thus, this module is as follows:

//the module file

module.exports.function_a = function (){ 
  //does stuff
  function_b()
};

module.exports.function_b = function_b = function () {
  //more stuff
}

I need to check function_a function with concrete result of function_b function.

I would like to override the function_b function from my test file, and then call the file__ function from my test file, as a result, the function_a function calls this override function instead of the function_b function.

Just notice, I tried and succeeded in redefining functions from individual modules, such as this question, but that doesn't interest me.

, , , . , , .

//test file
that_module = require("that module")
that_module.function_b = function () { ...override ... }
that_module.function_a() //now uses the override function

?

+4
1

exports. " " function_b . ( ) exports.function_b.

function_a exports.function_b function_b, , .

+4

All Articles