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

locals does not appear to set variable inside the module #45

Open
howarddierking opened this issue Dec 9, 2014 · 5 comments
Open

locals does not appear to set variable inside the module #45

howarddierking opened this issue Dec 9, 2014 · 5 comments

Comments

@howarddierking
Copy link

I'm trying to get locals working and suspect I'm doing something wrong as my scenario feels like it should be very basic.

In this simplified scenario, I have a test that should inject a local variable into my module which should override an existing variable. I then have an assertion that verifies the value was set.

mod.js looks like this

var foo = 'bar';

exports.getFoo = function(key){
    return foo;
};

and modTests.js looks like this

var SandboxedModule = require('sandboxed-module');

var mod = SandboxedModule.require('../lib/mod', {
    locals: { foo: 'baz' }
});

describe('mod', function(){
    it('should have value bar', function(){
        mod.getFoo().should.eql('baz');
    });
});

When I run the tests, I get an error that the function returns undefined. Not sure whether I'm doing something wrong here (I suspect so, as this seems like a really basic scenario) or if there's an issue in the code (I did step into it a bit, but it appeared that the local was at least collected from the options).

@domenic
Copy link
Collaborator

domenic commented Dec 9, 2014

You can't override existing variables with locals, only inject new ones.

@howarddierking
Copy link
Author

hmm - well, if I take away the module declaration of foo, I get the same result. Is there some difference in scope that is preventing exports.getFoo from reaching the injected variable?

@domenic
Copy link
Collaborator

domenic commented Dec 9, 2014

At that point I'm guessing it's a bug :(.

@howarddierking
Copy link
Author

ok - thanks for taking a look. Does it make sense to also add a feature request to enable overriding of existing variables? In my actual project, I'm looking for a way to fake out internal methods that coordinate more complex, I/O bound operations with a function that simulates latency. I can work around not having this capability by simply adding the function to the module's exports, but doing so adds otherwise unnecessary surface area to the module.

@domenic
Copy link
Collaborator

domenic commented Dec 9, 2014

I don't see how it would be possible to override the existing variables. The existing locals transform is supposed to work by transforming

// your code

into

(function (local1, local2) {
// your code
}(value1, value2));

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