jest-fs
Node.js fs module mock for Jest unit testing.
Requirements
- Node.js > 12
- Jest
Usage
First we need to create a __mocks__
folder and inside of the folder we must create a fs.js
file with the content:
const fs = require('jest-fs');
module.exports = fs;
After that, when creating a unit test we need to add these lines in the top:
const fs = require('fs');
// Add this
jest.mock('fs');
describe('testing jest-fs', () => {
it('should create a file', () => {
fs.writeFileSync('path/to/file.txt', 'content');
const files = fs.readdirSync('path/to');
expect(files.length).toBe(1);
});
});