-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Recipe for mocking the filesystem #665
Comments
Couldn't we reference the required |
We could, but we need to propagate that down to all the dependencies that might want to do that as well, including It would just be nice if |
Hmm yea, fair enough. |
I tried to test using this recipe. But I can't figure out what am I missing. Here is my test (with debug info) // compiler-template.js
import template from "lodash.template"
import { readFile } from "fs-promise"
export default function(filePath, templateVars) {
return readFile(filePath, { encode: "utf-8" })
.then((content) => template(content)(templateVars))
} // test
import test from "ava"
import mockFs from "mock-fs"
import mountfs from "mountfs"
import fs from "fs"
import compiler from "../compile-template"
mountfs.patchInPlace()
test("compile template with lodash.template", async (t) => {
fs.mount("/some/path", mockFs.fs({
"/template.js": "foo <%= bar %>",
}))
console.log(fs.readFileSync("/some/path/template.js").toString())
const result = await compiler("/some/path/template.js", { bar: "bar" })
t.is(result, "foo bar")
fs.umount("/some/path")
}) Log:
As you can see, I can read |
It looks like const mountfs = require('mount-fs');
mountfs.patchInPlace();
const compiler = require('../compile-template'); |
Well. After changing the test like you suggested. Test is passed. But when running AVA with nyc. The old issue is still there.
Maybe mocking fs with nyc is not a good idea. |
@thangngoc89 - can you upload a minimal reproduction to GitHub? It should contain the minimal amount of steps required to reproduce the problem. @novemberborn and I are maintainers on both the AVA and |
OK, I am stumped. It is only happening with |
1 downside of this recipe is it doesn't work with Windows. |
should I mock the filesystem to make this work: dotenv-safe do not work with ava |
@sibelius could you post your AVA configuration/ test code ? Mocking filesystem is a different problem here |
ava configuration "ava": {
"files": [
"./**/__tests__/**/*.blue.js"
],
"babel": "inherit",
"failFast": true,
"require": [
"babel-register",
"babel-polyfill"
]
}, I'm testing a koajs server using https://github.com/rolodato/dotenv-safe to require .env files it is failing in this line of dotenv-safe node_modules/dotenv-safe/index.js:26:42) (https://github.com/rolodato/dotenv-safe/blob/master/index.js#L26) |
@thangngoc89 this is a repo https://github.com/sibelius/koa-env-ava that reproduces this error |
fixed here: sibelius/koa-env-ava#1 tks |
Looks like this issue hasn't moved in a while. Does anyone have a working example of mocking the filesystem in AVA tests? |
Instead of monkey patching globals, node core modules or any other third party modules I usually do dependency injection. |
Dependency injection to the rescue (rewiremock with babel plugin) import test from 'ava';
// ^ needs a real FS
// nothing before would be mocked
import rewiremock from 'rewiremock/node';
import mockfs from 'mock-fs'; // mock-fs
import fs from 'fs'; // get-fs
rewiremock('fs').mockThrough(); // mock "fs"
// ^ this command would be hoisted by babel plugin
// this is FS, mocked by rewiremock and "hacked" by mockfs
// "real" FS is kept safe
fs.mount('/some/test/directory/', mockfs.fs({
'/some/test/directory': {
'some-file.txt': 'mock contents'
}
}); PS: With common js |
mock-fs
causes problems for AVA, but there are not great alternatives.mountfs
seems like a good solution. It could be used withmockFs.fs(..)
(which does not directly modify the nativefs
module).I haven't actually tested the above code, I just wanted to get the idea out there.
The text was updated successfully, but these errors were encountered: