TypeError: undefined is not a constructor TypeError: undefined is not a constructor angularjs angularjs

TypeError: undefined is not a constructor


undefined is not a constructor

is an error message PhantomJS displays when you tried to call a function that is not defined. It depends on the version of ECMAScript which your PhantomJS supports. So as you said it works fine in Chrome, because this browser supports the function you are using in test. In order to fix your problem and still be able to use PhantomJS you can replace the "unknown to PhantomJS" function with your own.


I was getting TypeError: undefined is not a constructor error using the includes() method. The includes() and endsWith() methods are new in ECMAScript 2015, and are not supported in Internet Explorer, and evidently not by PhantomJS.

There is a chance your end user may be using Internet Explorer. In which case, you may want to use the fully supported indexOf() method instead of includes() or endsWith()

For example, in my case, everything worked great in chrome, but my tests were failing on the line:

if (item.name.includes('contents'))

I changed to using the indexOf() method instead:

if (item.name.indexOf('contents') !== -1)

And then I was no longer getting the error

TypeError: undefined is not a constructor


I debated whether or not to post this as an answer or just an edit to my question, but I guess it's my answer (for now):

It seems the problem is related to PhantomJS. As soon as I changed the engine to Chrome in the karma.conf.js file, those tests passed.

I still don't know what that error message is supposed to mean and why it wasn't working with PhantomJS, but at least I'm now able to continue.

Here are the modifications to my karma.conf.js (in case anyone is curious):

browsers: [ //'PhantomJS', 'Chrome'],// Which plugins to enableplugins: [ 'karma-chrome-launcher', //'karma-phantomjs-launcher', 'karma-jasmine'],

Btw - I did notice that endsWith is new to ECMAScript6 (I was thinking it was older), but WebStorm shows that it's referencing a helper function in angular-ui-grid. I spent quite a while messing with the files array in the karma.conf.js file in an attempt to see if the ui-grid dependency was loading too late or something. In every test, it worked fine in Chrome but not PhantomJS. I still have no idea why.