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

TypeError: Class constructor Alg1 cannot be invoked without 'new' #119

Closed
rnemec opened this issue Nov 12, 2017 · 14 comments · Fixed by #123
Closed

TypeError: Class constructor Alg1 cannot be invoked without 'new' #119

rnemec opened this issue Nov 12, 2017 · 14 comments · Fixed by #123

Comments

@rnemec
Copy link

rnemec commented Nov 12, 2017

I have two classes: Alg1 extends Alg2.
When I rewire Alg2 and call its constructor new Alg2(),I get the error in the title.
Only fails on 3.0.0. Downgrading to 2.5.2 for now - all works fine there.

@JvJefke
Copy link
Contributor

JvJefke commented Nov 13, 2017

Could you give the exact error?

@rnemec
Copy link
Author

rnemec commented Nov 13, 2017

TypeError: Class constructor Alg1 cannot be invoked without 'new'
😄

@JvJefke
Copy link
Contributor

JvJefke commented Nov 13, 2017

I cannot reproduce this error. Would you be willing to give some example code where you experience this error? You're node version could also help :)

@rnemec
Copy link
Author

rnemec commented Nov 15, 2017

Node: 8.9.1
I'll try to find some time to create a reproducible example.

@rnemec
Copy link
Author

rnemec commented Nov 15, 2017

Here is the most trivial example. Three files in the same folder.
alg1.js:

class Alg1 {}
module.exports = Alg1;

alg2.js:

const Alg1 = require('./alg1');
class Alg2 extends Alg1 {}
module.exports = Alg2;

algtest.js:

const rewire = require('rewire');
const Alg = rewire('./alg2');
let a = new Alg();

Run node algtest, resulting in:

Alg2 = function (_Alg) {_inherits(Alg2, _Alg);function Alg2() {_classCallCheck(this, Alg2);return _possibleConstructorReturn(this, (Alg2.__proto__ || Object.getPrototypeOf(Alg2)).apply(this, arguments));}return Alg2;}(Alg1);
           ^

TypeError: Class constructor Alg1 cannot be invoked without 'new'
  at new Alg2 (/Users/rnemec/Projects/T/app/util/alg2.js:2:180)
  at Object.<anonymous> (/Users/rnemec/Projects/T/app/util/algtest.js:3:9)
  at Module._compile (module.js:635:30)
  at Object.Module._extensions..js (module.js:646:10)
  at Module.load (module.js:554:32)
  at tryModuleLoad (module.js:497:12)
  at Function.Module._load (module.js:489:3)
  at Function.Module.runMain (module.js:676:10)
  at startup (bootstrap_node.js:187:16)
  at bootstrap_node.js:608:3

@rnemec
Copy link
Author

rnemec commented Nov 15, 2017

@JvJefke - did the example help to reproduce? It's 3 files, total 8 lines of code, and it's 100% reproducible for me. Please, have a look

@JvJefke
Copy link
Contributor

JvJefke commented Nov 15, 2017

Thanks for the example!
I didn't have the time yet to look into this. I'll try to do it this evening.

@JvJefke
Copy link
Contributor

JvJefke commented Nov 15, 2017

I still can't reproduce this error. I added a test with your example, but it doesn't fail in node 8.9.1 on my computer.

see: JvJefke@35d4398

@rnemec
Copy link
Author

rnemec commented Nov 15, 2017

Hmm, weird.

I wonder - do you babelize your tests? What happens if you just run the test (or just a code that rewire-s and instantiates, like my 3rd file) from command line using plain node?

@JvJefke
Copy link
Contributor

JvJefke commented Nov 15, 2017

I also ran it from command line using plain node and it still worked.

Rewire uses babel internally to convert constants (const) to variables (var), but the tests itself aren't babelized.

Could you perhaps pull the bugfix/class-constructor branch from my fork (https://github.com/JvJefke/rewire/tree/bugfix/class-constructor) and run npm test to see if it issue is environment related or not?

@rnemec
Copy link
Author

rnemec commented Nov 15, 2017

Pulled the branch and npm test succeeds with 63 passing tests. Will play with it more to see what may be going on... One difference I see is that I'm using rewire as npm module, not running inside, like your tests.

@rnemec
Copy link
Author

rnemec commented Nov 15, 2017

Oh, found what is causing it: I have a .babelrc in the project root that is used for some other code. When I temporarily remove the babelrc, rewire works OK. It seems that your use of babel picks up the babel setting in my project (and those settings are not rewire-friendly, apparently).

Please, let me know if there is anything I can do to help resolving this...
Oh, my .babelrc is:

{
    "presets": ["es2015"],
    "plugins": ["transform-class-properties"]
}

@JvJefke
Copy link
Contributor

JvJefke commented Nov 15, 2017

I opened a PR to fix this: #123.
Thanks a lot for reporting this and helping us look for a solution!

@rnemec
Copy link
Author

rnemec commented Nov 15, 2017

My pleasure! Thanks guys for keeping this tool being one of top unit-testing utilities.

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

Successfully merging a pull request may close this issue.

2 participants