Skip to content

Instantly share code, notes, and snippets.

@fernandes
Created March 26, 2019 11:48
Show Gist options
  • Save fernandes/707c8c43827082ee558a8958f034a6a6 to your computer and use it in GitHub Desktop.
Save fernandes/707c8c43827082ee558a8958f034a6a6 to your computer and use it in GitHub Desktop.
AVA + mock-fs
import test from 'ava'
import fs from 'fs'
import mock from 'mock-fs'
test.before(t => {
mock({
'dir': {
'document': 'mocked content'
}
})
});
test.after.always(t => {
mock.restore()
});
test('fs sync', t => {
var contents = fs.readFileSync('dir/document', 'utf8')
t.is(contents, 'mocked content', 'mocked file content')
});
test('fs async', async t => {
const testReadFile = new Promise(function(resolve, reject) {
fs.readFile('dir/document', 'utf8', (err, data) => {
err ? reject(err) : resolve(data);
})
})
t.is(await testReadFile, 'mocked content', 'mocked file content')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment