Created
February 27, 2020 19:10
-
-
Save thybzi/9e387a28734e998740fb9ce6426e3ca5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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