Skip to content

Instantly share code, notes, and snippets.

@sgade
Last active August 29, 2015 13:56
Show Gist options
  • Save sgade/9238197 to your computer and use it in GitHub Desktop.
Save sgade/9238197 to your computer and use it in GitHub Desktop.
mock-fs module test example
// endsWidth, see http://stackoverflow.com/questions/280634/endswith-in-javascript
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
var fs = require('fs'),
path = require('path'),
mock = require('mock-fs');
function showText(file) {
if ( file.endsWith(".txt") ) {
console.log(file, "=>", fs.readFileSync(file, {
encoding: 'utf-8'
}));
}
}
function readDir(dir) {
if ( !dir ) dir = __dirname;
var files = fs.readdirSync(dir);
console.log(dir, "=>", files);
for ( var i = 0; i < files.length; i++ ) {
if ( files[i] == "node_modules" ) continue;
var subDir = path.join(dir, files[i])
var stat = fs.statSync(subDir);
if ( stat.isDirectory() ) {
readDir(subDir);
} else {
showText(subDir);
}
}
}
(function main(){
console.log("REAL");
readDir();
console.log("MOCK");
mock({
'mock.txt': 'MOCK!',
'home/': {
'subfolder': {
'welcome.txt': 'Welcome to the mock-fs.'
}
}
});
readDir();
mock.restore();
console.log("REAL");
readDir();
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment