In a JavaScript ES6-module, there may be many, small, easy-to-test functions that should be tested, but shouldn't be exported. How do I test functions in a module without exporting them? (without using Rewire).
function shouldntBeExportedFn(){
// Does stuff that needs to be tested
// but is not for use outside of this package
}
export function exportedFn(){
// A function that should be called
// from code outside of this package and
// uses other functions in this package
return shouldntBeExportedFn();
}
export const testables = {
shouldntBeExportedFn:shouldntBeExportedFn
}
Now
import {exportedFn} from './myPackage';
can be used in production code and
import {exportedFn, testables} from './myPackage';
const {shouldntBeExportedFn} = testables;
can be used in unit tests.
This way it retains the context clues for other developers on my team that shouldntBeExportedFn()
should not be used outside of the package except for testing.
I've been using this pattern for almost a year, and I find that it works very well.