thlorenz / proxyquireify

browserify >= v2 version of proxyquire. Mocks out browserify's require to allow stubbing out dependencies while testing.
MIT License
151 stars 24 forks source link

Cannot stub a property with a value of false #32

Closed DrewML closed 9 years ago

DrewML commented 9 years ago

To start with, thank you so much for the effort you've put into this module! It's exactly what I need for a current project.

I am running into one issue. In my project, I have a JSON file on disk that I require into modules. I'm attempting to use proxyquireify to overwrite values in this config. This has worked great, except for the use-case where I want to overwrite a value to false.

Example:

var stubs = {
    './config.json': {
        flag1: false, // true in config.json itself
        featureName: 'test name' // set to something else in config.json
    }
};

With the stubs above, I get the following behavior:

  1. featureName is set to 'test name' as expected
  2. flag1 is still true (should be false)

I'm guessing that proxyquireify does a truthy check when looping over each stub in a file, to check if it actually exists.

bendrucker commented 9 years ago

So this is handled correctly in proxyquire (Node) but not here. It was fixed fairly recently over there. And proxyquireify actually handles a case proxyquire doesn't (prototypes):

https://github.com/thlorenz/proxyquireify/blob/e05797491658b3658bed8cd745b715f7426cefcb/index.js#L42-L53

I'm going to pull the correct implementation out to a module and fix it there.

DrewML commented 9 years ago

@bendrucker You're fast! Thanks for identifying/submitting a PR for that so quickly. I really appreciate it.

kgryte commented 8 years ago

So the issue I reported over at https://github.com/bendrucker/proxyquire-universal/issues/18 actually seems to be related to this issue. When I set a stub to false, the stub is ignored, but, when I set the stub to a truthy value, the stub is used, even in the browser.

I am guessing this has to do with "simulating the absence of modules", but I was under the impression that setting a stub to null meant that the stub was ignored, not simply a falsey value.

Note: I am using proxyquirify@3.0.1.

bendrucker commented 8 years ago

You're totally right. The behavior is inconsistent w/ proxyquire. Will get a PR up now. This will most likely be a major bump since it's a bug fix in some sense but also a breaking change.

bendrucker commented 8 years ago

This is actually an entirely different issue though, let's move discussion over

kgryte commented 8 years ago

Sure. Send the link.