Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

module.exports multiple functions in Jest testing

After reading the Jest documentation, when it's mentioned that to export a single function from a tested file they show the following example:

function sum(a, b) {
  return a + b;
}
module.exports = sum;

Now, if I have multiple specific functions I want to export on my tested file, like this:

function sum(a, b) {
  return a + b;
}
function multiply(a, b) {
  return a * b;
}
function subtract(a, b) {
  return a - b;
}
module.exports = sum;
module.exports = multiply;

The multiply function is the only one being exported. How can I make these function be exported? Or only part of my file?

like image 430
gespinha Avatar asked Aug 30 '26 02:08

gespinha


1 Answers

You can do something like this :

module.exports = {};
module.exports.sum = function sum(a, b) {
  return a + b;
}
module.exports.multiply = function multiply(a, b) {
  return a * b;
}
module.exports.subtract = function subtract(a, b) {
  return a - b;
}

And you use it like this:

var MyMathModule = require('./my_math_module');
MyMathModule.sum(a, b);
MyMathModule.multiply(a, b);
MyMathModule.subtract(a, b);
like image 112
ndufreche Avatar answered Sep 01 '26 17:09

ndufreche