Skip to content

Instantly share code, notes, and snippets.

@thybzi
Created February 27, 2020 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thybzi/9e387a28734e998740fb9ce6426e3ca5 to your computer and use it in GitHub Desktop.
Save thybzi/9e387a28734e998740fb9ce6426e3ca5 to your computer and use it in GitHub Desktop.
function _patchFs() {
if (fs.hasOwnProperty('_mockedFiles')) {
return;
}
fs._readFileReal = fs.readFile;
fs._mockedFiles = {};
fs.mockFile = function(filePath, fileContent) {
fs._mockedFiles[filePath] = fileContent;
};
fs.readFile = function(filePath, cb) {
if (fs._mockedFiles.hasOwnProperty(filePath)) {
cb(null, Buffer.from(fs._mockedFiles[filePath]));
} else {
fs._readFileReal(...arguments);
}
};
}
function _unpatchFs() {
if (!fs.hasOwnProperty('_mockedFiles')) {
return;
}
fs.readFile = fs._readFileReal;
delete fs._mockedFiles;
delete fs.mockFile;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment