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')();
Description
TypeError: Class constructor … cannot be invoked without 'new'
is thrown when attempting to instantiate a class directly frommodule.__get__
. However, if the class is first bound to a local variable, such asMyClass = module.__get__('MyClass')
, the class can be instantiated.Reproduction
hasclass.js
dorewire.js
Context: