“The main goal for Karma is to bring a productive testing environment to developers”
— What amazing bulls**t, I want to say.
In my eight years of web programming experience nothing has caused more inconvenience and waste of time than setting up Karma tester configuration, even the initial setup of Webpack was easier.
Why is frontend testing with the Karma framework such a pain in the a**?
Essentially Karma doesn’t do anything by itself it doesn’t run a browser,
it just creates an abstract testing platform and has its own configuration.
If you just point file with tests in JavaScript language, Karma will just swear that it does not understand what it is and will not do anything.
If you need to start tests in Chrome browser you should add following line:
But that’s not all. To start Chrome browser, Karma needs plugin called karma-chrome-launcher. This means that Karma is made for starting test in browser and for starting this browser it needs some third-party plug-in. Why the possibility to start the browser is not included? It is not understandable.
Regarding above we add the following parameter into configuration:
Of course, it won’t work and you’ll see the following error:
I don’t know what the hell this error means. After searching the web I found out that Karma just doesn’t understand EcmaScript 6. Predictably.
“Karma is essentially a tool which spawns a web server that executes source code against test code for each of the browsers connected.”
To get ES6 code to work in the Chrome browser via Karma, you need to manually compile code from ES6 to ES5 using the browserify framework:
After launching, of course, we see the following error:
After searching in the web for some time I found, that in addition to browserify plugin we need babelify module to run ES6 code through Karma correctly. That is, to run ES6 code in browser we need double conversion of source code:
And of course after all the above manipulations still nothing will start. Again. Karma will print only following strange, incomprehensible error:
I do not have time to deal with endless bugs of the framework, which should not interfere, but on the contrary help me in development and simplify testing, saving my time. I would forgive disgusting official documentation of Karma, if it at least displayed some tips during setup, but it does not. It does nothing. It only complicates developer’s life by requiring connection of some modules and plugins, which written by other people, first part of which are abandoned and the second one have own bugs.
For example: Babel 7 has a problem with defining module dependencies and this problem is still not fixed (see issues on github).
You get the impression that you are in a situation where you have to build an electric drill from scratch to drill a couple of holes in the wall, instead of just buying it at the nearest store or borrowing it from a neighbor.
“The main goal for Karma is to bring a productive testing environment to developers. The environment being one where they don’t have to set up loads of configurations, but rather a place where developers can just write the code and get instant feedback from their tests.”
Oh really? If the Karma developers think this is a simple efficient framework, then what testing framework do they think is difficult?
Karma’s official website has no examples for Node.js or browsers, nothing at all. There are only the most rudimentary examples of setting up documentation, everything else is scattered all over the site. You have to learn and build everything from scratch, because there are no ready-made standard solutions.
A search on the Internet for this error gave no results, so after several hours of futile attempts to solve it, reinstalling all and everything, I just gave up on this. Karma and switched to Mocha.
Mocha
Fortunately, Mocha is much more flexible, easy to configure and much less capricious testing framework, which is a complete alternative and competitor to Karma. Easy setup, good documentation, lots of examples, and compatibility with TypeScript make Mocha still relevant today.
And even Jest, which Facebook is trying so desperately to promote, cannot yet compete on an equal footing with Mocha, because it lacks many plugins and flexibility.
Mocha just works.
It just works. Exactly.