Is it possible to run Karma with no browsers at all?
Asked Answered
T

3

40

I started an Angular.js app with Yeoman's yo angular that includes Karma testing. Then, the grunt test reasonably failed because Karma can't find any browsers. (The browsers has not been set in the app's node_modules/karma config file.)

I'm doing my development via SSH into a remote machine, which I think (let me know if I'm wrong) is pointless to have Chrome, /usr/bin/chromium-browser, installed.

So is it possible to run Karma without any browsers?

Tollefson answered 20/2, 2014 at 1:1 Comment(5)
Why not make use of PhantomJS instead of Chrome?Reiners
Good point. I think I'll get that. But is it possible to run Karma without any browsers?Tollefson
No, Karma does need a browser.Reiners
Thanks for the confirmation. You can put that comment as an answer and get points. Btw, your quote is so encouraging: "The biggest satisfaction for a great programmer is when he doesn't understand." :)Tollefson
Yes :) Very encouraging ;)Reiners
R
23

Karma needs a browser to be set.

You can make use of PhantomJS instead of Chrome.
Indeed, it's more discreet than a traditional browser launch.

Reiners answered 20/2, 2014 at 9:35 Comment(5)
This comment is posted on 2014, it still needs browser ?Professor
Yes it still requires it. You might want to consider Mocha if you don't want any browser.Reiners
Keep scrolling...the real answer is down below.Peba
PhantomJS is no longer maintained. Avoid to use it.Textualism
Im suffering from the consequences now, upgrading to Angular 9. Use ChromeHeadlessChobot
B
45

I am going to add my two cents to this.

Correct - Karma requires a browser to run. BUT - you can run Chrome in Headless mode, which means although you do need the browser installed, it will not open it's UI, and you can therefore run the tests purely through an SSH session for example.

We used this configuration for our CI/CD deployments. Our Docker image for running the tests had Chrome installed and we ran them with Chrome headless mode. Worked like a charm.

To use this, simply modify your browsers property in your karma.conf.js

browsers: ['ChromeHeadless']

Hope this might help someone out there who may be looking for something similar...

Beeswax answered 12/5, 2019 at 0:53 Comment(1)
ChromeHeadless is the answer. This should be at the top.Tibetoburman
R
23

Karma needs a browser to be set.

You can make use of PhantomJS instead of Chrome.
Indeed, it's more discreet than a traditional browser launch.

Reiners answered 20/2, 2014 at 9:35 Comment(5)
This comment is posted on 2014, it still needs browser ?Professor
Yes it still requires it. You might want to consider Mocha if you don't want any browser.Reiners
Keep scrolling...the real answer is down below.Peba
PhantomJS is no longer maintained. Avoid to use it.Textualism
Im suffering from the consequences now, upgrading to Angular 9. Use ChromeHeadlessChobot
J
13

This question and answer is very relevant as of today (soon 2018, > angular2, @angular/cli, typescript, ...).

Here is a small update, based on what i found as useful on the net:

Say you have an angular cli generated project that has not been tempered with. Say you want to use PhantomJS to run your angular2 tests (nothing shocking).

Start by installing PhantomJS launcher for Karma in your project

npm i --save-dev karma-phantomjs-launcher

Next you have to update the karma.conf.js file as follows:

First the the plugins properties:

plugins: [
  require('karma-jasmine'),
  require('karma-phantomjs-launcher'),
  require('karma-jasmine-html-reporter'),
  require('karma-coverage-istanbul-reporter'),
  require('@angular/cli/plugins/karma')
],

Then the browser properties

browsers: [ 'PhantomJS' ],

Running the test at this point, you will probably stumble on the following error:

PhantomJS 2.1.1 (Windows 7 0.0.0) ERROR
  TypeError: pre,template,textarea,script,style is not iterable!
  at http://localhost:9876/_karma_webpack_/polyfills.bundle.js:792

Basically, that means your PhantomJS needs different polyfills. Uncomment the following lines in your src\polyfills.ts

import 'core-js/es6/symbol';
import 'core-js/es6/object';
import 'core-js/es6/function';
import 'core-js/es6/parse-int';
import 'core-js/es6/parse-float';
import 'core-js/es6/number';
import 'core-js/es6/math';
import 'core-js/es6/string';
import 'core-js/es6/date';
import 'core-js/es6/array';
import 'core-js/es6/regexp';
import 'core-js/es6/map';
import 'core-js/es6/weak-map';
import 'core-js/es6/set';

Last but not least, here are the links that helped me work this out:

How to run jasmine tests without browser ?

New angular-cli app not working on iphone 4s

Judicatory answered 15/12, 2017 at 10:58 Comment(3)
Avoid using it, PhantomJS is not supported anymoreDisfeature
@LevKolomazov what is an alternative then?Cowgirl
@MarcusCantu npmjs.com/package/karma-jsdom-launcherPejsach

© 2022 - 2024 — McMap. All rights reserved.