Last active
December 24, 2018 14:39
-
-
Save fernandes/f865251a7310346bc1f17e5fe46c744f to your computer and use it in GitHub Desktop.
AVA test with mock-fs, reading file sync and async
This file contains hidden or 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
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