Skip to content

Instantly share code, notes, and snippets.

@digitalkaoz
Last active October 18, 2017 15:23
Show Gist options
  • Save digitalkaoz/2a8c876a2bfeede3dd7db21b913e2b60 to your computer and use it in GitHub Desktop.
Save digitalkaoz/2a8c876a2bfeede3dd7db21b913e2b60 to your computer and use it in GitHub Desktop.
Jest + Filesystem mocks
//your files
module.exports = {
"src/a.js": `const b = require("./b");module.exports = class A {}`,
"src/b.js": "module.exports = class B {}",
};
//__mocks__/fs.js
module.exports = require("memfs");
jest.mock("fs"); // we need this if some of your code uses "fs", so we make sure they use the mocked variant
const { fs, vol } = require("memfs");
const fixtures = require("./fixtures/files");
describe("fs", () => {
beforeEach(() => {
vol.fromJSON(fixtures, `${__dirname}/fixtures`);
});
afterEach(() => {
vol.reset();
});
it("can operate on virtual in memory files", () => {
expect(fs.existsSync(`${__dirname}/fixtures/src/a.js`).toBeTruthy();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment