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)
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)
Jest vs Karma/Jasmine which testing library you will prefer to use and why?
What is difference between Jasmine and karma? - Ask a Question - TestMu AI Community
jquery - standalone Jasmine vs Karma - Jasmine - Stack Overflow
Angular testing techniques Jasmine vs karma vs protractor in Angular 2? - Stack Overflow
Videos
Karma and Jasmine's SpecRunner.html are both test runners (aka spec runners). The difference between the two is that Karma is an application that runs outside the browser, while SpecRunner is a normal HTML file with a bunch of script references that you open up in a browser.
A test runner that lives outside the browser gives you a number of benefits:
- automatically run tests in one or multiple browsers at once
- automatically run tests on file changes, without having to manually refresh the browser
- file pre-processing, i.e.
- compiling TypeScript to JavaScript before running tests
- inlining HTML templates into JavaScript
- file/report generation, i.e. test coverage reports
- test framework flexibility
- easily include/exclude test and source files using file and folder patterns rather than trying to wrangle 100s of
<script>references in a HTML file
I haven't used jasmine-jquery, but for jasmine tests with Karma, Karma uses the karma.conf.js to discover external dependencies (such as jasmine-jquery). Particularly the files property. Some nice examples are here If you're running jasmine tests with jasmine's SpecRunner.html, you need to make sure anything you use is linked in the SpecRunner.html with script tags.
As for testing click handlers, one good bet might be just to call the click handler functions directly. It sounds like if you want something more realistic, you're getting closer to functional testing. For that you might consider incorporating nightmare to automate user interactions such as clicks, etc.
Jasmine is a behavior-driven development framework for testing JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM. And it has a clean, obvious syntax so that you can easily write tests.
Karma is essentially a tool for testing which spawns a web server that executes source code against test code for each of the browsers connected. The results of each test against each browser are examined and displayed via the command line to the developer such that they can see which browsers and tests passed or failed.
Jasmine and Karma are usually used together to perform Unit testing or integration testing.
Protractor is an end-to-end test framework for Angular and AngularJS applications. Protractor runs tests against your application running in a real browser, interacting with it as a user would without depending on other tools for performing the same. Check out how it works here.
References:
Jasmine Documentation
Karma - How it works?
Protractor
Karma is test runner like grunt and gulp Jasmine is a BDD framework for testing
You can write expectations i.e. tests in jasmine and run it using Karma.
Protractor does this both the things for you.