Unit testing JavaScript with Karma in 2021. Starting point 🌻

npm install --save-dev karma
npx karma init
Do you want to use Require.js ?
no
Do you want to capture any browsers automatically ?
Press tab to list possible options. Enter empty string to move to the next question.
> Chrome
What is the location of your source and test files ?
You can use glob patterns, eg. "js/*.js" or "test/**/*Spec.js".
Enter empty string to move to the next question.
> src/**/*.test.js
> src/**/*.js
Should any of the files included by the previous patterns be excluded ?
You can use glob patterns, eg. "**/*.swp".
Enter empty string to move to the next question.
>
Do you want Karma to watch all the files and run the tests on change ?
Press tab to list possible options.
> yes
Config file generated at "/<project>/karma.conf.js".
+    "karma-chrome-launcher": "^3.1.0",
+ "karma-jasmine": "^4.0.1",
  • karma-chrome-launcher the plugin which helps run tests in Chrome
  • karma-jasmine an adapter for jasmine
npm install jasmine
describe('My first Karma test', function() {
it('should be true', function() {
expect(true).toEqual(true);
});
});
npx karma start
30 01 2021 14:59:09.329:WARN [karma]: No captured browser, open http://localhost:9876/
30 01 2021 14:59:09.353:INFO [karma-server]: Karma v6.0.3 server started at http://localhost:9876/
30 01 2021 14:59:09.353:INFO [launcher]: Launching browsers Chrome with concurrency unlimited
30 01 2021 14:59:09.358:INFO [launcher]: Starting browser Chrome
30 01 2021 14:59:10.799:INFO [Chrome 88.0.4324.96 (Mac OS 11.1.0)]: Connected on socket kj6wu3FYg7XPKEKCAAAB with id 15222593
Chrome 88.0.4324.96 (Mac OS 11.1.0): Executed 1 of 1 SUCCESS (0.002 secs / 0.002 secs)
TOTAL: 1 SUCCESS
  1. Karma starts the server
  2. Open http://localhost:9876 in Chrome
  3. Run tests and print the result
it('should be true', function() {
expect(true).toEqual(false);
});
30 01 2021 15:02:37.443:INFO [filelist]: Changed file "/playground/src/foo.test.js".
Chrome 88.0.4324.96 (Mac OS 11.1.0) My first Karma test should be true FAILED
Error: Expected true to equal false.
at <Jasmine>
at UserContext.<anonymous> (src/foo.test.js:3:18)
at <Jasmine>
Chrome 88.0.4324.96 (Mac OS 11.1.0): Executed 1 of 1 (1 FAILED) (6.226 secs / 0.001 secs)
TOTAL: 1 FAILED, 0 SUCCESS
  • Name of a test suite. In our case My first Karma test
  • Name of a test case — test should be true
  • Expectation/assert error message — Expected true to equal false
  • Exception stack trace
singleRun: true,
npx karma start --no-single-run
npx karma run
function multiply(x, y) {
return x * y;
}
it('should multiply 2 on 2', function() {
expect(multiply(2, 2)).toEqual(4);
});
Chrome 88.0.4324.96 (Mac OS 11.1.0): Executed 2 of 2 SUCCESS (0.006 secs / 0 secs)
TOTAL: 2 SUCCESS

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store