40

I've been trying to run my tests using karma-chrome-launcher, but everytime I run my tests it throws this error:

INFO [launcher]: Starting browser Chrome
ERROR [launcher]: Cannot start Chrome
INFO [launcher]: Trying to start Chrome again (1/2).
ERROR [launcher]: Cannot start Chrome   
INFO [launcher]: Trying to start Chrome again (2/2).
ERROR [launcher]: Cannot start Chrome
ERROR [launcher]: Chrome failed 2 times (cannot start). Giving up.

Here's my karma.conf.js code:

// Karma configuration
// Generated on Mon Mar 23 2015 14:04:19 GMT-0300 (BRT)

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: 'www',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine'],


    // list of files / patterns to load in the browser
    files: [
      'lib/ionic/js/angular/angular.js',
      'lib/ionic/js/angular/angular-animate.js',
      'lib/ionic/js/angular/angular-sanitize.js',

      '../node_modules/jasmine-core/lib/jasmine-core/jasmine.js',
      '../node_modules/mock-local-storage/lib/mock-localstorage.js',
      '../node_modules/angular-mocks/angular-mocks.js',
      //'../node_modules/requirejs/require.js',
      'lib/ionic/js/angular/angular-resource.js',
      'lib/ionic/js/angular-ui/angular-ui-router.js',
      'lib/ionic/js/ionic.js',
      'lib/ionic/js/ionic-angular.js',
      /*'../tests/libs/ngCordovaMocks.min.js',*/
      'js/lib/ng-cordova.min.js',
      'js/*.js',
      'js/controllers/*.js',
      'js/services/*.js',
      'js/factory/*.js',
      //'../tests/*.js',
      '../tests/**/*.js'
    ],


    // list of files to exclude
    exclude: [
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress', 'html'],

    htmlReporter: {
      outputFile: '../tests/report/index.html'
    },


    // web server port
    port: 9876,

    plugins : [
      'karma-junit-reporter',
      'karma-jasmine',
      'karma-phantomjs-launcher',
      'karma-chrome-launcher'
      //'karma-htmlfile-reporter'
    ],

    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['PhantomJS'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false
  });
};

I'm installing the module here: https://www.npmjs.com/package/karma-chrome-launcher

Thanks!

Yuri
  • 4,254
  • 1
  • 29
  • 46
SupimpaAllTheWay
  • 1,308
  • 3
  • 16
  • 22
  • install chrome launcher again and use karma init to regenerate config file – samyak bhalerao Sep 25 '15 at 05:24
  • 4
    Were you able to solve your problem? I got the same problem and I haven't been able to solve it yet. – VaTo Nov 04 '15 at 20:25
  • Hey @SaulOrtega, I don't remember exactly what I did but what helped me a lot is to use karma-phantom-launcher instead of Chrome, I recommend you to give a try :) – SupimpaAllTheWay Nov 05 '15 at 18:23
  • Thank you @Dyego, I'm a little confused here, are you talking about changing that in the configuration file? – VaTo Nov 05 '15 at 19:29
  • Only if you're using Chrome inside "browsers", I might be wrong here but this code " browsers: ['PhantomJS']," is requiring PhantomJS (google it for more info). So what I'm saying is that don't use karma-chrome-launcher anymore, instead use karma-phantom-launcher (which uses PhantomJS). It's working great for me since. Is that more clear? – SupimpaAllTheWay Nov 06 '15 at 00:41
  • Summarizing... use PhantomJS inside "browsers" remove karma-chrome-launcher and install phantomjs and karma-phantomjs-launcher using npm :) – SupimpaAllTheWay Nov 06 '15 at 00:45
  • 1
    We were experiencing a lot of timeout-like issues with Karma and eventually we did what Karma does - gave up. We switched to `jest` and the nightmare has ended. I suggest you take a look: https://www.xfive.co/blog/testing-angular-faster-jest/ – JeB Mar 09 '18 at 19:27

16 Answers16

28

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.

Yuri
  • 4,254
  • 1
  • 29
  • 46
Gerros
  • 688
  • 11
  • 20
9

Had the same issue with my build environment.

What i did is to follow the advice of Rafael Cichocki to enable the debugging:

logLevel: config.LOG_DEBUG

Then tried to launch the chrome-browser with exactly the same line that was visible int he debug output.

Turned out that chrome browser was crashing due to missing ttf fonts. So running:

apt-get install ttf-freefont

Solved that issue for me and karma started to launch chrome.

Vladimir M
  • 4,403
  • 1
  • 19
  • 24
5

In Karma.conf.js, Increase timeouts to 60000

captureTimeout: 60000,
browserDisconnectTimeout: 60000,
browserDisconnectTolerance: 3,
browserNoActivityTimeout: 60000,
Yuri
  • 4,254
  • 1
  • 29
  • 46
Kurkula
  • 6,386
  • 27
  • 127
  • 202
3

browsers: ['PhantomJS'], Here allowed browser is PhantomJs, but code is trying for Chrome, which is not a specified browser in the karma.conf.js.

Change karma.conf.js file :

browsers: ['PhantomJS','Chrome', 'ChromeHeadless'],

chrome- is for opening new chrome browser window.

ChromeHeadless- is for running tests without opening browser window

make sure Chrome is installed and added to PATH

Hope this helps

2

I noticed when I had this error that when I changed the spec file and saved it, it seemed to work again. I had a few errors in typescript that didn't break the tests (passing in null arguments to a virtual component instance constructor). I don't know if it was resolving the errors since they existed before when it was working, or if changing the file and saving it updated the cache.

So this could mean that clearing the cache in Chrome could also potentially resolve it. It's working now again for me so I can't check to verify.

David
  • 21
  • 3
2

Just in case you are running this behind a corporate proxy. Make sure you include your 0.0.0.0 in your NO_PROXY Environment variable.

Otherwise your test will first go out through your firewall where it will most likely not be able to reach 0.0.0.0. So just to be sure I include the following in my

NO_PROXY=127.0.0.1,localhost,0.0.0.0

Especially if you are running your tests in a container environment (e.g. your build pipeline) non set environment variables might be a common reason for ng test working fine on your local machine but failing to connect to google-chrome in the container.

relief.melone
  • 3,042
  • 1
  • 28
  • 57
2

If someone faces this error only in gitlab-runner (but in shell by hand ng test work ok), you can apply decision from here: https://forum.gitlab.com/t/running-karma-tests-with-chrome-and-gitlab-ci/14476

The decision is: in karma.config.js, replace the section

browsers: ['Chrome'],

by

browsers: ['ChromeHeadlessNoSandbox'],
customLaunchers: {
  ChromeHeadlessNoSandbox: {
    base: 'ChromeHeadless',
    flags: ['--no-sandbox']
  }
},

The reason of error is that Chrome doesn't support no-sandbox anymore

Laser42
  • 646
  • 2
  • 7
  • 24
1

I got my inspiration partially from here: https://stackoverflow.com/a/33802985/1534823

Also use logLevel: config.LOG_DEBUG - it can help you get good information on what is causing your error`

Check following settings in karma.conf:

captureTimeout: 60000,
browserNoActivityTimeout: 360000
browser: ["Firefox"]
  • captureTimeout - your browser may take some time to start. LOG_DEBUG should show some error related to capturing your browser
  • browserNoActivityTimeout - PhantomJS is really slow(x10) on my machine, in comparison to Firefox and Chrome. Karma may timeout before your tests complete.
  • browser - our jenkins server runs on linux, where we had no binaries for chrome, so we had to switch to firefox

If any of these three settings were not set correctly, we would get the error you described above.

Community
  • 1
  • 1
Rafael Emshoff
  • 2,541
  • 1
  • 35
  • 47
1

I was able to resolve this by remove the absolute path (src/examplePath) and changing it to a relative path (../../examplePath).

Example change in spec:

import { myPackage } from 'src/myPath'; (seems to be the issues)

import { myPackage } from '../../../myPath'; (seems to resolve it)

Note I had tried deleting the node modules and npm installing but that didn't work. I'm so not sure why this matters.

RamenChef
  • 5,557
  • 11
  • 31
  • 43
Brian
  • 179
  • 7
1

In Windows Chrome was install to %LOCALAPPDATA%/Google/Chrome/Application earlier. Now it's install to %PROGRAMFILES%/Google/Chrome/Application. If you is very long time with Chrome then your have old version in %LOCALAPPDATA%/Google/Chrome/Application.

Karma-launcher is searches Chrome location in order LOCALAPPDATA->PROGRAMFILES-> 'PROGRAMFILES(X86)' , first found old version and try to run it.

Just delete %LOCALAPPDATA%/Google/Chrome/Application folder

alezhu
  • 475
  • 3
  • 6
0

Solution for us with angular cli was setting the following properties in the karma.conf.js

 autoWatch: false,
 singleRun: true
Alexander
  • 3,019
  • 1
  • 20
  • 24
0

I was also facing this issue. I made below three changes in my karma.config.js file.

 autoWatch: false,
 browsers: ['Chrome'],
 singleRun: false
Himanshu Shekhar
  • 1,196
  • 1
  • 16
  • 35
0

I experienced this after updating macOS to Catalina. I solved it by updating Puppeteer to the latest version.

Alex Steinberg
  • 1,426
  • 13
  • 25
0

What worked for me:

  • npm un karma-chrome-launcher

  • npm i karma-chrome-launcher

  • npm i -g karma-cli

  • karma init (and follow the prompts)

Ryan
  • 11
  • 1
0
ng test --watch=false

Using the watch flag set to false in combination with adjustment of the following parameters in karma.config.js worked for me:

browsers: ['ChromeHeadlessNoSandbox'],
customLaunchers: {
  ChromeHeadlessNoSandbox: {
    base: 'ChromeHeadless',
    flags: ['--no-sandbox']
  }
},
Shane Rich
  • 63
  • 6
0

I faced a similar issue recently.

And found two solutions to fix this problem.

  1. I installed puppeteer and added process.env.CHROME_BIN = require('puppeteer').executablePath() at the top of my karma.conf.js file, as documented here.
  2. I uninstalled Google Chrome and cleared up all the system folders (%Local Appdata%\Google and also from Program Files / Program Files x86), restarted by the system, and then installed Chrome x64 from the official site.

I was happy with the 1st solution as well, but since I wanted to fix the root problem, I went ahead and found the second solution as well.

Hope this solves the problem for anyone facing this issue.

Ayushya
  • 1,862
  • 1
  • 10
  • 15