Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Karma vs testing framework Jasmine, Mocha, QUnit [closed]

People also ask

What is the difference between Mocha and Jasmine framework?

Jasmine was created around 2008. The documentation describes Jasmine as “batteries included,” meaning that it attempts to provide everything a developer needs in a test framework. Mocha is younger than Jasmine, created around 2011. Mocha is not a “complete” test framework, and doesn't attempt to be.

Is Karma a test framework?

Karma isn't a testing framework. So, it isn't a competitor to tools like Mocha, QUnit, or Jasmine, to name a few. Instead, Karma works with them, so you'll have to pick a testing framework to use. There are available plugins for the most well-known testing frameworks.

Which is better Mocha or Jasmine?

Mocha is significantly more flexible and comes with a test runner, but you have to piece it yourself. In the Angular world, Jasmine is the recommended testing framework. This is because Angular CLI, by default, comes with Jasmine and Karma as the test runner.

What is the difference between karma and Jasmine?

Jasmine can be classified as a tool in the "Javascript Testing Framework" category, while Karma is grouped under "Browser Testing". "Can also be used for tdd " is the primary reason why developers consider Jasmine over the competitors, whereas "Test Runner" was stated as the key factor in picking Karma.


Karma is a browser test runner.

The idea is that browsers do not have natively a concept of loading tests files, running them, and reporting results. What karma does is (roughly) :

  • starting a small web server to serve "client-side" javascript files to be tested (1)
  • also serve the "client-side" javascript files with the tests (or Specs, as they're often called) (2)
  • serve a custom web page that will run javascript code for the tests (3)
  • start a browser to load this page (4)
  • report the results of the test to the server (5)
  • karma can then again report the results to text files, the console, anything your CI server likes, etc...

Looking at each part :

(1) Those files will be your actual js files ; you will tell karma how to load them. If you use requirejs, there is a karma plugin, and some config is needed.

(2) Those tests can be written in a variety of Javascript testing framework (Jasmine, QUnit, Mocha) ; this is JS code that is run in the browser.

(3) The custom web page will be a bit different for each testing framework ; this is why karma has plugins for different frameworks.

(4) Karma can launch the page in many browsers (FF, Chrome, or headless browsers like PhantomJs.)

(5) Reporting to karma is, again, framework-dependant, and dealt with karma plugins.

So to answer your questions :

  • in Java, most people use JUnit which is both a framework to write tests and run them, but does not have the problem of differentiating the environment in which tests are run and the one in which test reports are aggregated ; karma would be the missing piece between a JUnit Suite and a JUnit TestRunner
  • Yes, you can do everything that karma does "by hand" - pick one framework (jasmine, qunit, mocha) and follow instructions. The advantage of karma is that it provide a solution out-of-the-box, if you're in a standard setup.
  • Karma can be used for both unit test (with jasmine / qunit / whatever) and integration tests (which will use another API, like webdriver, to drive the browser)

One shorter way to understand the difference:

People testing with plain Jasmine / Mocha most likely are running all the code with the Node virtual machine.

Adding Karma to the mix (on top of your existing framework of choice) will run your test suite with the engine of other browsers.

By doing this you get the little extras you get with a browser environment. It will be easier to test DOM related code, but you will also be giving up to the extra resources given by the Node engine (like filesystem / shell access)


The thesis of the guy who designed Karma was very informative in describing existing solutions and comparing them, and of course describing Karma itself

https://github.com/karma-runner/karma/blob/master/thesis.pdf

Summary: Karma is a test runner. It can be used by QUnit, Jasmine, Mocha, ... Karma has advantages to other test runners to improve your TDD/BDD development cycle. It "watches" files, so when you save a change, Karma runs tests and reports back instantly, no switching context to Web Browser to run the test.

In short, perhaps question should be Karma AND Jasmine or Mocha or QUnit?