jhnns / rewire

Easy monkey-patching for node.js unit tests
MIT License
3.08k stars 128 forks source link

Cannot instantiate class directly from module.__get__ #142

Open justinlovinger opened 6 years ago

justinlovinger commented 6 years ago

Description

TypeError: Class constructor … cannot be invoked without 'new' is thrown when attempting to instantiate a class directly from module.__get__. However, if the class is first bound to a local variable, such as MyClass = module.__get__('MyClass'), the class can be instantiated.

Reproduction

hasclass.js

class Foo {

}

dorewire.js

const rewire = require('rewire');
const hasclass = rewire('./hasclass.js');

// No error when class if first bound to a local variable
const Foo = hasclass.__get__('Foo');
const foo1 = new Foo();

// Throws error if class is not bound to a local variable
const foo2 = new hasclass.__get__('Foo')();

Context: