Git Product home page Git Product logo

Comments (4)

jhnns avatar jhnns commented on May 10, 2024

Hi,

unfortunately not, because JavaScript copies primitives like numbers by value. This means, that an assignment creates a copy of the original value. Changing the original value won't change the copy.

For instance:

var a = 2;
var b = a; // assigns the value 2, not the reference a

a = 3;
console.log(b); // 2

Objects (and arrays) on the other hand are copied by reference, so:

var obj1 = {
   foo: "foo"
};
var obj2 = obj1; // assigns the reference of obj1 to obj2

obj1.foo = "bar";
console.log(obj2.foo); // "bar"

from rewire.

jhnns avatar jhnns commented on May 10, 2024

Unfortunatly there is no operator to change this behaviour. I think they made this to keep JavaScript simple for beginners. But actually most of the time it's no real problem, it's only annoying if you want to copy an object.

from rewire.

sporto avatar sporto commented on May 10, 2024

Right, I tried to simplify the example too much making it different from my original use case and thus introduced the passed by value issue without noticing.

This is closer to what I would like to do, maybe this is possible:

var foo = require('./foo');
var bar = require('./bar');

var providers = {
    foo: foo,
    bar: bar
}

module.exports = {
    run: function (provider) {
        return providers[provider];
    }
};

So I have an object providers that has references to two modules. I want to dynamically call one of these modules depending on the params passed into my module.

So in my test I would like to rewire foo and get this line return providers[provider] to return the fake version.

thanks

from rewire.

jhnns avatar jhnns commented on May 10, 2024

If you just want to rewire providers.foo you could do:

var subject = rewire('../../services/foo');
subject.__set__("providers.foo", fooMock);

or of course rewire the whole providers-object.

Since the code is executed when calling rewire(), the reference of the original foo-module is copied instantly to the providers-object. So rewiring the foo-module won't change the copied reference. I don't see any other way than manually rewiring every reference in the module.

You could also take a look at injectr. There you can provide mocks for certain require()-calls. 😄

from rewire.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.