Last active
October 18, 2017 15:23
-
-
Save digitalkaoz/2a8c876a2bfeede3dd7db21b913e2b60 to your computer and use it in GitHub Desktop.
Jest + Filesystem mocks
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
//your files | |
module.exports = { | |
"src/a.js": `const b = require("./b");module.exports = class A {}`, | |
"src/b.js": "module.exports = class B {}", | |
}; |
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 | |
module.exports = require("memfs"); |
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
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