Export an “exportedForTesting” const
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
}
export const exportedForTesting = {
shouldntBeExportedFn
}
The following can be used in production code:
import { exportedFn } from './myPackage';
And this can be used in unit tests:
import { exportedFn, exportedForTesting } from './myPackage';
const { shouldntBeExportedFn } = exportedForTesting;
This strategy 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 for years, and I find that it works very well.