Created
September 21, 2016 16:15
-
-
Save heyimalex/5b2f637c2a890c01f0876725cf1a848c to your computer and use it in GitHub Desktop.
Using mock-fs with jest
This file contains 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
// __tests__/example.js | |
jest.mock('fs'); | |
it('should serve as a nice example', () => { | |
const fs = require('fs') | |
// fs can be set up at any point by calling __configureFs. | |
fs.__configureFs({ | |
'/test': { | |
'a.foo': '----', | |
// mock-fs' other exports are also available on the fs object. | |
'b.bar': fs.__mockFile({ | |
content: 'file content here', | |
ctime: new Date(1), | |
mtime: new Date(1) | |
}), | |
}) | |
const results = fs.readdirSync('/test') | |
expect(results.length).toBe(2) | |
expect(results).toEqual(['a.foo', 'b.bar']) | |
}) |
This file contains 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
// __mocks__/fs.js | |
const mockfs = require('mock-fs'); | |
const container = { | |
__internalFs: null, | |
__configureFs: (conf) => { | |
container.__internalFs = mockfs.fs(conf); | |
}, | |
__mockFile: mockfs.file, | |
__mockDirectory: mockfs.directory, | |
__mockSymlink: mockfs.symlink, | |
} | |
const proxyfs = new Proxy(container, { | |
get: function (target, property, receiver) { | |
if (target.hasOwnProperty(property)) { | |
return target[property]; | |
} else { | |
if (target.__internalFs === null) { | |
target.__internalFs = mockfs.fs(); | |
} | |
return target.__internalFs[property]; | |
} | |
} | |
}) | |
module.exports = proxyfs; |
This file contains 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
// mock-fs hooks the native fs on first require, so unfortunately just using | |
// the __mocks__ file isn't enough; jest will mock fs itself and stuff gets | |
// complicated. I'm actually not completely sure _what_ happens, but the end | |
// result is that you need to require mock fs before anything else or it | |
// doesn't work. | |
require('mock-fs') |
This file contains 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
{ | |
"devDependencies": { | |
"jest": "^15.1.1", | |
"mock-fs": "^3.11.0", | |
}, | |
"scripts": { | |
"test": "jest" | |
}, | |
"jest": { | |
"setupFiles": ["<rootDir>/jestsetup.js"] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@digitalkaoz For what it's worth, @cesalberca's solution works exactly as expected. In my case I use sync access and it works like a charm.
Here are my dependencies :
NB : I create an in-memory mock beforeEach assertion, but I call manually mock.restore() in my test, since it needs to happen at different times (before file access to assert the Error thrown, after file access to assert the success).