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
How to mock modules that are required with 'enhanced-require' #30
Comments
enhanced-require creates a own request function, so the mocked one created by 'mockery' or 'sandboxed-module' will be overwritten. I cannot support these modules because they will break if they want to resolve the parameter of the required function (i. e. But I'm working on enhanced-require 0.4.x for a while and that will change some stuff. You do not longer need to add the (ugly) header into each file. You can enable it once (like sandboxed-module). You create a require function with some options and use it. All modules required will have it recursivly enabled. The options are similar to the webpack options so you can use the var enhancedRequire = require("enhanced-require"); // 0.4.x
var myRequire = enhancedRequire(module, {
recursive: true,
resolve: {
alias: {
// provide a mock by new resolving alias
"module": "module-mock"
}
}
});
// inject a mock into the cache
myRequire.cache[myRequire.resolve("../lib/file")] = {
exports: { abc: "abc" }
};
var subject = myRequire("../lib/subject"); It's not (yet) perfect, but I try to add some some stuff to the wip 0.4.x version to support this (with better syntax). Maybe something like this: var er = require("enhanced-require");
it("should do something", function() {
var subject = er.recursive(module, {
replacements: {
"../lib/file": { exports: { abc: "abc" }},
"module": "module-mock" // this is not exactly the same as above
}
})("../lib/subject");
subject.should.have.property("file").be.eql("abc");
} NOTE: keep in mind that enhanced-require 0.4.x is still beta and API may change. You can file more issue at http://github.com/webpack/enhanced-require |
Thanks for the quick response … looks very promising ... If I understand correctly, enhanced-require 0.4.x would then completely replace the sandboxed-module, correct ? I quickly prototyped what you are suggesting into 0.3.x and indeed, this seems to work well, provided, as you already mentioned, enhanced-require can be applied as a global config instead of calling it in every source file. The prototype adds a sandbox config to the enhanced-require options. require = require("enhanced-require")(module, {
sandbox: {
requires: {
'./append': '',
'raw!../assets/hello.txt': 'Hello',
'raw!../assets/webPack.txt': 'WebPack',
'raw!../assets/world.txt': 'World'
}
}
}); Has been tested with the following small test example: module.exports = function (done) {
require = require("enhanced-require")(module, { sandbox: {
requires: {
'./append': '',
'raw!../assets/hello.txt': 'Hello',
'raw!../assets/webPack.txt': 'WebPack',
'raw!../assets/world.txt': 'World'
}
}});
var append = require('./append');
var hello = require('raw!../assets/hello.txt');
var webPack = require('raw!../assets/webPack.txt');
var helloWebPack = append(hello, webPack);
require.ensure(['raw!../assets/world.txt'], function (require) {
var world = require('raw!../assets/world.txt');
var helloWebPackWorld = append(helloWebPack, world);
done(helloWebPackWorld);
});
return helloWebPack;
} The implementation of the prototype only requires a few small changes to RequiredContent.js. In what state is 0.4.x, is it stable enough to try it out ? |
Yes it apply it recursivly if you add You find You can put it into a fork and I'll take a look. {
substitutions: { // define the exports of a module
'raw!../assets/hello.txt': 'Hello',
'raw!../assets/webPack.txt': 'WebPack',
'raw!../assets/world.txt': 'World'
// the key is resolved with require.resolve,
// so any require matching this file will be replaced
}
} |
Apparently I was already using wip-0.4 and not 0.3 :-). Forked enhanced-require to: https://github.com/kavaro/enhanced-require.git Made following changes to wip-0.4: (all changes are marked with // Kavaro comment)
Note: to run tests you need to first npm install enhanced-resolve and raw-loader |
Here is the documentation. |
Both 'mockery' and 'sandboxed-module' are unable to mock out modules that have been required with 'enhanced-require'. Please advise how to mock such modules.
The text was updated successfully, but these errors were encountered: