tschaub / mock-fs

Configurable mock for the fs module
https://npmjs.org/package/mock-fs
Other
907 stars 85 forks source link

Question: Is it supports mock file by function dynamic? #313

Open imcuttle opened 4 years ago

imcuttle commented 4 years ago

like

mockFs({
    '/path/filename': (options) => {
         return fs.readFileSync('/a/b/c', options)
    }
})
3cp commented 4 years ago

mock.load with lazy option. https://github.com/tschaub/mock-fs#loading-real-files--directories

imcuttle commented 4 years ago

@3cp Is it could be more functional? like below

mockFs({
    '/path/filename': (options) => {
         return `import foo from 'foo';\n${fs.readFileSync('/a/b/c', options)}`
    }
})
3cp commented 4 years ago

No api for that now. What's that "options"?

3cp commented 4 years ago

I mean who will pass options to that func?

3cp commented 4 years ago

If no need options, you can use standard getter (not mockfs feature of-course) to delay the evaluation of file content;

const mockfs = require('mock-fs');
const fs = require('fs');

mockfs({
  get ['file/name']() {
    return 'hello';
  }
});

const content = fs.readFileSync('file/name', 'utf8');
console.log(content);

Update: get 'file/name'() { works too.