1

I am currently working through this AngularJS tutorial (https://docs.angularjs.org/tutorial/) and have encountered an interesting problem when attempting to run unit tests using 'npm test'. This is described at step 2 of the tutorial (https://docs.angularjs.org/tutorial/step_02).

My setup is as follows:

Problem: The tutorial discusses unit tests and says to execute these with 'npm test' command. This will trigger the opening of either the Chrome or Firefx browser and then run the tests. So far so good. But: when I run the 'npm test' command in my Putty ssh shell, there is (obviously) no Chrome or FF browser window that can be opened. Hence, I get this error:

vagrant@homestead:~/Code/angular-phonecat$ npm test

> angular-phonecat@0.0.0 pretest /home/vagrant/Code/angular-phonecat
> npm install

npm WARN install:nopt@3.0.6 EPROTO: protocol error, symlink '../nopt/bin/nopt.js' -> '/home/vagrant/Code/angular-phonecat/node_modules/.bin/nopt'

> angular-phonecat@0.0.0 postinstall /home/vagrant/Code/angular-phonecat
> bower install


> angular-phonecat@0.0.0 test /home/vagrant/Code/angular-phonecat
> node node_modules/karma/bin/karma start test/karma.conf.js

INFO [karma]: Karma v0.12.37 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
ERROR [launcher]: No binary for Chrome browser on your platform.
  Please, set "CHROME_BIN" env variable.
INFO [launcher]: Starting browser Firefox
ERROR [launcher]: Cannot start Firefox

INFO [launcher]: Trying to start Firefox again (1/2).
ERROR [launcher]: Cannot start Firefox

INFO [launcher]: Trying to start Firefox again (2/2).
ERROR [launcher]: Cannot start Firefox

ERROR [launcher]: Firefox failed 2 times (cannot start). Giving up.

Note that this is not the same as the types of problems reported in these posts:

Setting environment variable for CHROME_BIN does not work

How to fix error "Please set env variable CHROME_BIN" when running Angular.js with grunt

These posts (and there may be others) assume that the npm test tool (Karma?) is used on a Windows machine, which has Chrome and FF installed. But I am remotely logging on to a Ubuntu machine, which has FF installed and the FIREFOX_BIN environment variable set correctly (/usr/bin/firefox), but cannot open a visual windows because I am looged on through a commnd line shell.

So, my question is: how can I use npm test (or Karma) to run my unit tests using the setup described above?

Unfortunately I don't know how to ask my question or state the problem any better than this, so if you need any other details to help me, please let me know.

Thanks AHL

Community
  • 1
  • 1
AHL
  • 738
  • 3
  • 11
  • 35

1 Answers1

3

Instead of running tests on Chrome / Firefox, run them against PhantomJS, a headless browsers.

npm install --save-dev phantomjs-prebuilt karma-phantomjs-launcher

In your karma.conf.js:

  • to the plugins, add karma-phantomjs-launcher
  • instead of browsers: ['Chrome'] set browsers: ['PhantomJS']

You will also have to add some polyfills for PhantomJS (which is lacking the bind function), for that, use : https://www.npmjs.com/package/phantomjs-polyfill (it's explained how to use it with karma)

topheman
  • 7,422
  • 4
  • 24
  • 33