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

Problem rewiring exported class that uses composition #162

Open
edwmurph opened this issue Apr 17, 2019 · 0 comments
Open

Problem rewiring exported class that uses composition #162

edwmurph opened this issue Apr 17, 2019 · 0 comments

Comments

@edwmurph
Copy link

edwmurph commented Apr 17, 2019

To recreate this problem, create the following 3 files, and run node test.js.

Rewiring the "Car" module again after changing the composed "Engine" instance returns a fresh Car instance, but the rewired change to the composed "Engine" via __set__ is persisted in the "fresh" instance.

// Engine.js
class Engine {
  start() {
    return 'engine started';
  }
}

module.exports = new Engine();
// Car.js
const engine = require('./Engine');

class Car {
  constructor( engine ) {
    this.engine = engine;
  }
}

const car = new Car( engine );

module.exports = car;
// test.js
const rewire = require('rewire');

const car1 = rewire('./Car')
console.log( 'car1.engine.start():', car1.engine.start() );

car1.__set__('car.engine.start', () => 'changed');
console.log( 'car1.engine.start():', car1.engine.start() );
 
const car2 = rewire('./Car')
console.log( 'car2.engine.start():', car2.engine.start() );

console.log(car1 === car2);

/* outputs:
car1.engine.start(): engine started
car1.engine.start(): changed
car2.engine.start(): changed
false
*/
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

1 participant