fake-module-system
v0.3.0
Published
A mock module system for testing require extensions
Downloads
52
Maintainers
Readme
fake-module-system
A mock module system for testing require extensions.
Install
$ npm install --save-dev fake-module-system
Usage
import System from 'fake-module-system';
const system = new System({
'./foo.js': 'bar' // a file named foo.js with content "bar"
});
// alternate method instead of setting up content in the constructor.
system.content['./foo.js'] = 'bar';
let module = system.load('./foo.js');
// A module is not actually evaled, or ran. It just stores the code and filename
assert(module.code === 'bar');
assert(module.file === './foo.js');
// You can add custom require extensions for testing (the whole point of this module).
// This unrealisticly simple one just adds "quz" at the end of every file.
system.extensions['.js'] = appendQuzTransform();
module = system.load('./foo.js');
assert(module.code === 'barquz');
// Provides convenience method for installing a simple transform.
// This is handy for verifying how your extension behaves with earlier or later extensions.
system.installTransform((code, filename) => {
if (shouldTransform(filename)) {
return filename + ' foo';
}
});
myTransformUnderTest.install(system.extensions);
Related:
- How require extensions work, step by step.
License
MIT © James Talmage