Recently I needed to expand the exports in a file to all methods in order to allow for Jest tests to cover more of the code (without using Rewire). Beforehand, only the functions used by other production files were exported, meaning unit tests only covered the exposed methods and not the non-exported ones. I'm wondering if there are any disadvantages to exporting all of the functions within a file - or if there is a better way that this code can be written such that I can unit test all of the methods within individually. I made a simple example of the code in question:
Calculator.js
const sum = (addend1, addend2) => addend1 + addend2;
const difference = (minuend, subtrahend) => subtrahend - minuend;
const product = (multiplicand, multiplier) => multiplicand * multiplier;
const quotient = (dividend, divisor) => dividend / divisor;
const findAbsDifference = (number1, number2) => difference(Math.abs(number1), Math.abs(number2));
export {
sum,
product,
quotient,
findAbsDifference
} // difference is not exported, because it's not used in any other file
Calculator.test.js
import * as Calculator from "./Calculator.js";
describe('Calculator', () => {
test('sum(5, 5)', () => {
const addend1 = 5;
const addend2 = 5;
const expectedSum = 10;
expect(Calculator.sum(addend1, addend2)).toEqual(expectedSum);
});
test('findAbsDifference(-5, 2)', () => {
const minuend = -5;
const subtrahend = 2;
const expectedDiff = 3;
expect(Calculator.findAbsDifference(minuend, subtrahend)).toEqual(expectedDiff);
});
/**
*
* I'd like to test the difference(minuend, subtrahend) method here,
* although I am unable to do this unless it is exported.
* Is there any disadvantage to exporting this function when it's only used in the test
* and within findAbsDifference which is in the same file?
*
**/
test('product(11, 2)', () => {
const multiplicand = 11;
const multiplier = 2;
const expectedProduct = 22;
expect(Calculator.product(multiplicand, multiplier)).toEqual(expectedProduct);
});
test('quotient(20, 5)', () => {
const dividend = 20;
const divisor = 5;
const expectedQuotient = 4;
expect(Calculator.quotient(dividend, divisor)).toEqual(expectedQuotient);
});
});