What is mock-fs?
The mock-fs npm package allows you to mock the file system in Node.js, enabling you to test code that interacts with the file system without actually performing any real file system operations. This is particularly useful for unit testing and ensuring that your code behaves correctly in different file system scenarios.
What are mock-fs's main functionalities?
Mocking a Simple File System
This feature allows you to create a simple mock file system with directories and files. The `mock` function takes an object that represents the file system structure. After setting up the mock file system, you can run your code that interacts with the file system. Finally, you call `mock.restore()` to clean up and restore the real file system.
const mock = require('mock-fs');
mock({
'path/to/fake/dir': {
'some-file.txt': 'file content here',
'empty-dir': {}
}
});
// Your code that interacts with the file system
mock.restore();
Mocking File System Errors
This feature allows you to simulate file system errors, such as permission errors. In this example, the `mock.file` function is used to create a file with no permissions. When attempting to read the file, an error is thrown, which you can catch and handle in your code.
const mock = require('mock-fs');
const fs = require('fs');
mock({
'path/to/fake/dir': {
'some-file.txt': mock.file({
content: 'file content here',
mode: 0o000 // no permissions
})
}
});
try {
fs.readFileSync('path/to/fake/dir/some-file.txt');
} catch (err) {
console.error('Error reading file:', err);
}
mock.restore();
Mocking a Large File System
This feature allows you to create a large mock file system with many files. The `mock.directory` function is used to create a directory with a large number of files. This is useful for testing how your code handles large file systems.
const mock = require('mock-fs');
mock({
'large-dir': mock.directory({
items: Array.from({ length: 1000 }, (_, i) => ({
[`file-${i}.txt`]: `content of file ${i}`
})).reduce((acc, item) => Object.assign(acc, item), {})
})
});
// Your code that interacts with the large file system
mock.restore();
Other packages similar to mock-fs
memfs
The memfs package provides an in-memory file system that can be used for testing and other purposes. Unlike mock-fs, which mocks the Node.js file system module, memfs provides a complete in-memory file system implementation that can be used as a drop-in replacement for the real file system. This can be useful for more complex scenarios where you need a fully functional file system.
fs-mock
The fs-mock package is another library for mocking the file system in Node.js. It provides a simpler API compared to mock-fs and is designed for basic use cases. While it may not have all the features of mock-fs, it can be a good choice for simpler scenarios where you just need to mock a few files and directories.
Mock FS
A configurable mock file system. You know, for testing.
Purpose
Instead of testing against real file system resources, it should be easy to mock up a file system for tests.
Example
The code below creates a mock fs
module that is configured to work with a few mock files and directories.
var mock = require('mock-fs');
var fs = mock.fs({
'path/to/fake/dir': {
'some-file.txt': 'file content here',
'empty-dir': {}
},
'path/to/some.png': new Buffer([8, 6, 7, 5, 3, 0, 9]),
'some/other/path': {}
});
If you are testing a module that require
s the real fs
module, you can use rewire
to inject a mock fs
module for testing.
var rewire = require("rewire");
var moduleToTest = rewire('./path/to/module');
moduleToTest.__set__('fs', fs);
Status
The table below shows what is covered by the mock fs
. Tests are currently run on Node 0.8 and 0.10. Eventually more versions will be supported.
function | status | notes |
---|
fs.Stats | 90% | Incudes mode , size , atime , ctime , mtime , isFile() , and isDirectory() |
fs.exists | 100% | Complete |
fs.existsSync | 100% | Complete |
fs.close | 0% | Implement binding.close |
fs.closeSync | 0% | Implement binding.close |
fs.open | 0% | Implement binding.open |
fs.openSync | 0% | Implement binding.open |
fs.read | 0% | Implement binding.read |
fs.readSync | 0% | Implement binding.read |
fs.write | 0% | Implement binding.write |
fs.writeSync | 0% | Implement binding.write |
fs.rename | 0% | Implement binding.rename |
fs.renameSync | 0% | Implement binding.rename |
fs.truncate | 0% | Implement binding.ftruncate |
fs.ftruncate | 0% | Implement binding.ftruncate |
fs.ftruncateSync | 0% | Implement binding.ftruncate |
fs.rmdir | 0% | Implement binding.rmdir |
fs.rmdirSync | 0% | Implement binding.rmdir |
fs.fdatasync | 0% | Implement binding.fdatasync |
fs.fdatasyncSync | 0% | Implement binding.fdatasync |
fs.fsync | 0% | Implement binding.fsync |
fs.fsyncSync | 0% | Implement binding.fsync |
fs.mkdir | 0% | Implement binding.mkdir |
fs.mkdirSync | 0% | Implement binding.mkdir |
fs.readdir | 0% | Implement binding.readdir |
fs.readdirSync | 0% | Implement binding.readdir |
fs.fstat | 0% | Implement binding.fstat |
fs.lstat | 0% | Implement binding.lstat |
fs.stat | 90% | Provides stats object described above |
fs.fstatSync | 0% | Implement binding.fstat |
fs.lstatSync | 0% | Implement binding.lstat |
fs.statSync | 90% | Provides stats object described above |
fs.readlink | 0% | Implement binding.readlink |
fs.readlinkSync | 0% | Implement binding.readlink |
fs.symlink | 0% | Implement binding.symlink |
fs.symlinkSync | 0% | Implement binding.symlink |
fs.link | 0% | Implement binding.link |
fs.linkSync | 0% | Implement binding.link |
fs.unlink | 0% | Implement binding.unlink |
fs.unlinkSync | 0% | Implement binding.unlink |
fs.fchmod | 0% | Implement binding.fchmod |
fs.fchmodSync | 0% | Implement binding.fchmod |
fs.chmod | 0% | Implement binding.chmod |
fs.chmodSync | 0% | Implement binding.chmod |
fs.fchown | 0% | Implement binding.chown |
fs.fchownSync | 0% | Implement binding.chown |
fs.utimes | 0% | Implement binding.utimes |
fs.utimesSync | 0% | Implement binding.utimes |
fs.futimes | 0% | Implement binding.futimes |
fs.futimesSync | 0% | Implement binding.futimes |