Skip to content
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

Closed
kavaro opened this issue Nov 3, 2012 · 5 comments
Closed

How to mock modules that are required with 'enhanced-require' #30

kavaro opened this issue Nov 3, 2012 · 5 comments

Comments

@kavaro
Copy link

kavaro commented Nov 3, 2012

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.

@sokra
Copy link
Member

sokra commented Nov 4, 2012

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. require("loader!./file")). So there is no support with enhanced-require 0.3.x.


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 alias option to mock modules. Or you can inject modules into the cache.

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

@kavaro
Copy link
Author

kavaro commented Nov 4, 2012

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.
The sandbox config object is a sandboxed-module config (only implements requires), see example:

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.
If you are interested to get the source, just let me know ...
(Didn't know how to attach the source files to this post)

In what state is 0.4.x, is it stable enough to try it out ?
Does it already recursively apply enhanced-require to all modules ?

@sokra
Copy link
Member

sokra commented Nov 4, 2012

Yes it apply it recursivly if you add recursive: true to the options. So you can add it into the test case and remove it from the module.

You find wip-0.4 on enhanced-require and enhanced-resolve and wip-0.8 on webpack. They currently work well, but I think the HCR API is not yet stable enougth.

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
  }
}

@kavaro
Copy link
Author

kavaro commented Nov 4, 2012

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)

  1. lib/require.js: add sandbox default options
  2. lib/RequireContext.js: add sandbox capability
  3. examples/simple-app: directory with a simple test app, used by test/simple_app.js
  4. test/simple_app.js: test case

Note: to run tests you need to first npm install enhanced-resolve and raw-loader

sokra added a commit to webpack/enhanced-require that referenced this issue Nov 5, 2012
@sokra
Copy link
Member

sokra commented Nov 5, 2012

Here is the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants