50

I'm trying to set up Karma to run AngularJS unit tests using Jasmine, but I can't get the tests to run. I'm sure I'm overlooking something simple. I'm running this on a Windows 7 machine with Node.js installed and karma installed via npm.

My directory structure looks like this:

  • js/app/ - contains controllers, app, etc
  • js/config/ - contains karma.conf.js
  • js/lib/ - contains angular
  • js/test/ - contains jasmine specs

I'm starting a command prompt in the js directory and running this command:

karma start config/karma.conf.js

That causes Chrome to run on port 9876, but whenever I change any watched files and check the Karma output, I see this info message:

No captured browser, open http://localhost:9876/

Here's my config file:

module.exports = function(config) {
  config.set({
    basePath: '../',
    frameworks: ['jasmine'],
    files: [
      'lib/angular.js',
      'app/**/*.js',
      'test/**/*.js'
    ],
    exclude: [
    ],
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    captureTimeout: 60000,
    singleRun: false
  });
};

I'm using Angular 1.2.10 and Karma 0.10.9

levelnis
  • 7,665
  • 6
  • 37
  • 61
  • 1
    It seems that there's some issues with Karma on Windows : did you try the solutions suggested here : https://github.com/karma-runner/karma/issues/635 ? It could be related to the path to chrome binary (Karma only know the default path), it could be your firewall (try to disable it for a while), you should also look at your host file. – glepretre Feb 04 '14 at 08:08
  • Thanks @glepetre but none of those suggestions have worked unfortunately. My hosts file is clean (fresh install of Windows) and switching off the firewall didn't help. I can get the browser to load (tried Firefox, Chrome and IE) but it just says `Karma - starting` and then doesn't show any further output in the browser. – levelnis Feb 08 '14 at 23:00
  • 1
    I can confirm same problem happens on Mac. I don't understand why a browser needs to be opened for a unit test which is not necessarily linked to a ui. –  Mar 05 '14 at 11:10
  • 1
    I think they're opened because the test framework makes direct use of the javascript runtime engine within the browser. Frustrating that it doesn't seem to work and nobody has an answer though – levelnis Mar 05 '14 at 11:58
  • Check https://stackoverflow.com/questions/34286210/how-to-run-karma-tests-from-docker-container/72306681#72306681 if you are facing this in Docker. – Nishant May 19 '22 at 14:49

13 Answers13

14

I had a face palm moment. karma start needs to be run in the same directory as your karma.conf.js file.

AlignedDev
  • 8,102
  • 9
  • 56
  • 91
  • This fixed it for me, too. Honestly, I don't think that should be a face palm moment for you. It's a pretty misleading error message from Karma. – Nick Heiner Jan 05 '18 at 23:07
  • If Karma is not run from the same folder as the configuration file it's possible to pass the path to the configuration file e.g. `karma start path/to/karma.conf.js` – Malice Aug 28 '18 at 13:54
11

I just had the same issue in a different setup (Linux, QUnit, Firefox), though. The problem disappeared after I killed all karma processes and did a fresh karma start.

user1460043
  • 2,331
  • 1
  • 19
  • 26
7

This error can mean that the browser can't find the server. Check if you can reach the server at the URL it mentions. It could be a misconfigured port number, or even (as it was in my case), localhost being misconfigured. I suppose it could be the server is not running, or any of a dozen other problems.

Check if you can reach the server manually. If you can't, fix that.

mcv
  • 4,217
  • 6
  • 34
  • 40
5

In my case the environment variable CHROME_BIN was set to a wrong path. Fix the value (might be defined in /etc/environment) or unset the environment variable temporarily with unset CHROME_BIN.

To find the cause of the problem it was helpful to set log level to debug in Karma's config file:

logLevel: config.LOG_DEBUG,
splash
  • 13,037
  • 1
  • 44
  • 67
4

I had the same problem and it was a bug in a test. Removing that code fixed the error.

Laszlo Sarvold
  • 733
  • 7
  • 15
  • I'm also pretty sure its doing this due to some bug in a spec.ts file... However I have no idea how to track it... – Johan Aspeling Aug 22 '19 at 09:35
  • 1
    Found it, there was a typo in the import of my one file. Building didnt pick it up, serving didnt pick it up, running `ng serve` didn't pick it up... – Johan Aspeling Aug 22 '19 at 09:57
3

This happened to me when I had an endless loop in my code. Commenting out (using xdescribe in jasmine) recent code I wrote got the tests running again.

yndolok
  • 5,197
  • 2
  • 42
  • 46
2

I had the same problem and tried a lot of the suggested solutions I found, but what finally solved it for me was to delete the node_modules folder and getting everything new via npm install.

same issue: Karma - Chrome failed 2 times (cannot start). Giving up

Gerros
  • 688
  • 11
  • 20
1

I had the same problem, no code changes, just re-running tests that worked a month ago.

When I ran from webstorm, it seemed to be taking a long time to capture the browser, so I wondered if it was timing out.

So I tried setting browser to just Chrome (i was using phantomjs as well), and waiting. Then reset back to Chrome and PhantomJS, now it's fine.

Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
MarkSh
  • 129
  • 1
  • 3
1

I had the same problem and I did all what everyone from here said without any result. In my case I didn't do the init:

C:\Users>cd c:\xampp\htdocs\angularjs-pro\ch07

c:\xampp\htdocs\angularjs-pro\ch07>karma init

Which testing framework do you want to use ?
Press tab to list possible options. Enter to move to the next question.
> jasmine

Do you want to use Require.js ?
This will add Require.js plugin.
Press tab to list possible options. Enter to move to the next question.
> 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

The second time when I got in the same situation was when I tried to do karma start in the wrong folder, not where I put karma init.

In both cases after I fixed the problem I got in widows console:

22 11 2016 14:06:51.697:INFO [karma]: Karma v1.3.0 server started at http://localhost:9876/
22 11 2016 14:06:51.698:INFO [launcher]: Launching browser Chrome with unlimited concurrency
22 11 2016 14:06:51.711:INFO [launcher]: Starting browser Chrome
22 11 2016 14:06:53.936:INFO [Chrome 54.0.2840 (Windows 10 0.0.0)]: Connected on socket /#HnJUQrkRPqtGFa5ZAAAA with id 16058274
1

Installing karma-cli npm install -g karma-clisolved this issue for me. Hope this helps.

exp
  • 39
  • 6
1

I was facing the same issue and i resolved it by killing all the chrome instances from Task Manager. It seems that there were 'n' number of test runners running. When you close the karma test runner without stopping the server, it's instance is still alive.

So press 'alt + ctrl + delete', kill all sessions/instances of chrome and restart Karma.

It works!

Rahul Sharma
  • 329
  • 1
  • 3
  • 10
1

Had the same error recently:

  • a project using angular-cli,
  • tests running on Jenkins in a headless environment.

Turns out I have cancelled a Jenkins job previously in the step where the tests were running, so ng test did not quit (had a handful of running ng processes, paired with [node] <defunct> ones, blocking the further test execution). Solution was to find and kill all hanging ng processes:

$ sudo -su jenkins
# ps aux | grep "ng$" | awk '{print $2}' | xargs kill -9
hoefling
  • 59,418
  • 12
  • 147
  • 194
1

I had the same message because there was no Chrome installed on my PC. Just Chromium. Well, it turns out that's not the same. Now after installing Chrome, both 'Chrome' and 'ChromeHeadless' browsers are able to start.

Otherwise you might specify 'Chromium' or 'ChromiumHeadless' in config and be fine with it.

TLDR: Make sure you have actually installed the browser you are testing in.

Anton Rusak
  • 882
  • 4
  • 18