2017-09-06 00:35:30 +02:00
|
|
|
const fs = jest.genMockFromModule('fs');
|
|
|
|
|
|
|
|
// This is a custom function that our tests can use during setup to specify
|
|
|
|
// what the files on the "mock" filesystem should look like when any of the
|
|
|
|
// `fs` APIs are used.
|
|
|
|
let mockFiles = Object.create(null);
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-underscore-dangle
|
|
|
|
function __setMockFiles(newMockFiles) {
|
|
|
|
mockFiles = newMockFiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A custom version of `readdirSync` that reads from the special mocked out
|
|
|
|
// file list set via __setMockFiles
|
|
|
|
const readFileSync = (filePath = '') => mockFiles[filePath];
|
2020-03-27 20:04:50 +01:00
|
|
|
const existsSync = (filePath) => !!mockFiles[filePath];
|
|
|
|
const lstatSync = (filePath) => ({
|
2020-02-12 13:41:28 -08:00
|
|
|
isFile: () => !!mockFiles[filePath],
|
|
|
|
});
|
2017-09-06 00:35:30 +02:00
|
|
|
|
|
|
|
// eslint-disable-next-line no-underscore-dangle
|
|
|
|
fs.__setMockFiles = __setMockFiles;
|
|
|
|
fs.readFileSync = readFileSync;
|
|
|
|
fs.existsSync = existsSync;
|
2020-02-12 13:41:28 -08:00
|
|
|
fs.lstatSync = lstatSync;
|
2017-09-06 00:35:30 +02:00
|
|
|
|
|
|
|
module.exports = fs;
|