Last active
July 21, 2018 11:38
-
-
Save gaspaonrocks/2f9ec9ac458bbbc4dc4c037284e5de7f to your computer and use it in GitHub Desktop.
using mock-fs with jest to assert the result of file system operations in a sync manner
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
import mock from 'mock-fs'; | |
import fs from 'fs'; | |
import fileHandler from '../utils/fileHandler'; | |
beforeEach(() => { | |
// Creates an in-memory file system | |
mock({ | |
'/test': { | |
'note.md': 'hello world!' | |
} | |
}); | |
}); | |
it('throws error if path doesn\'t exist', () => { | |
mock.restore(); // => called here to check if Error is thrown | |
const error = () => fileHandler.saveFileSync('/test/note2.md', 'ciao world!'); | |
expect(error).toThrow(); | |
}); | |
it('saves a file given it\'s contents', () => { | |
fileHandler.saveFileSync('/test/note2.md', 'ciao world!'; | |
expect(fs.existsSync('/test/note.md')).toBeTruthy(); | |
mock.restore(); // => called here as in a regular afterEach | |
}); | |
it('throws error if path doesn\'t exist', () => { | |
mock.restore(); // => called here to check if Error is thrown | |
const error = () => fileHandler.loadFileSync('/test/note.md'); | |
expect(error).toThrow(); | |
}); | |
it('loads a file from disk', () => { | |
const actual = fileHandler.loadFileSync('/test/note.md'); | |
expect(actual).toBe('hello world!'); | |
mock.restore(); // => called here as in a regular afterEach | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment