For example in clean-directory.js
we have the following function:
export default function cleanDirectory() {
const files = fs.readdirSync(DIR_TO_CLEAN).map((f) => path.join(DIR_TO_CLEAN, f));
files.forEach((file) => {
fs.unlinkSync(file);
});
return files.length;
}
We want mock out the contents of the filesystem per test.
-
Dependency injection. We could have
fs
orreaddirSync
andunlink
be default arguments to thecleanDirectory
function:export default function cleanDirectory(_readdirSync = readdirSync, _unlink = unlink) {
Given the availability of battle-tested modules to mock out the file system, I think it's a nicer development experience to use one of those (e.g. memfs, mock-fs) - rather writing our own
readdirSync
andunlink
functions each time and littering our function interface.