Your Karma sux – “Undefined is not a constructor.”

Oh yeah it does.

So we need tests for this new project. And vue-cli lets you create scaffold a new project in no time, running command in your terminal and hitting Enter a few times. It provides you with a sample app with pre-configured tests that use Karma (browser launcher), mocha (testing framework) and PhantomJS (browser that the tests are run in)

All is good so we port our component that we create a component and try to test it. It all works well. Since it’s our first scaffolded project, we are fooling around a bit with the settings and not using git. Yay, and a fail. So after an hour or so trying to pile new things on top of it to just try them out, the project becomes a mess. We leave it so.

We create a scaffolded app. Then we move the code from the component and the code for its tests. They just worked on the old one, but don’t work on the new app. How come? No idea… as good programmers we started to look for the fault on our side. Maybe the test code we wrote is not good enough. Maybe the tests are not. When we ran the tests we only got this mysterious error stating:

“Undefined is not a constructor.”

Well… same code, different app, everything is crashing. So as “proper” programmers we start to search the answer on google. (Someone said “I get paid to Google stuff”, that was one of those moments). So half an hour later we are still nowhere near finding a solution. There are a ton of threads on the thing and it seems a generic JS error, not really connected to tests in any way. After an hour or two of this nonsense I decided to just install a fresh scaffolded app (number 3 so far). Then build everything line by line while running the tests at every step so I see exactly when I break them. I also put it all in a git repo, including the node_modules folder. Oh yeah, baby!

Some time later I am done rewriting the tests line by line and it all works. It turns out that the previous app was so broken that it was not able to run the tests properly! Not only that but we found some people on the internets complaining that sometimes their Karma installs don’t work. But sometimes they do. So in the end it looks like it’s all about luck.

Get your karma, install it, if it doesn’t work, install it again.. And yes.. Maybe you would like to use git for all of it, just to make sure some files don’t get corrupted or something.

  • Johnny Driesen

    And at the end … we all still love programming 🙂 🙂 🙂