Search code examples
angularunit-testingtestingkarma-jasminekarma-coverage

Please set env variable CHROME_BIN | Windows


karma can not start chrome - here is error

I tried to ng test for my angular application and I get this result. I tried to use this command in cmd:

SET CHROME_BIN = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"

My karma config file:

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage'),
      require('@angular-devkit/build-angular/plugins/karma'),
      require('karma-junit-reporter')
    ],
    client: {
      jasmine: {
        // you can add configuration options for Jasmine here
        // the possible options are listed at https://jasmine.github.io/api/edge/Configuration.html
        // for example, you can disable the random execution with `random: false`
        // or set a specific seed with `seed: 4321`
      },
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    jasmineHtmlReporter: {
      suppressAll: true // removes the duplicated traces
    },
    coverageReporter: {
      dir: require('path').join(__dirname, './coverage/xxxx'),
      subdir: '.',
      reporters: [
        { type: 'html' },
        { type: 'text-summary' }
      ]
    },
    reporters: ['progress', 'kjhtml', 'junit', 'coverage'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false,
    restartOnFileChange: true,
    junitReporter: {
      outputDir: 'junit',
      outputFile: 'junit-result.xml',
      useBrowserName: true,
      nameFormatter: undefined,
      classNameFormatter: undefined,
      properties: {}
    }
  });
};

I restart pc and tried that command but it didn't work. I have chrome.exe in this path, does anyone know a solution?

Note: I am not a linux user..


Solution

  • If you're encountering an issue with Karma and ChromeHeadless not finding a binary, there are a few steps you can take to address this issue:

    Install or Reinstall ChromeHeadless: Make sure you have Chrome or Chromium installed on your system. If you don't, you may need to install it. For example, in Ubuntu, you can install Chromium with sudo apt-get install chromium-browser.

    Specify the Chrome Binary Path: In your Karma configuration file (usually karma.conf.js), you can specify the path to the Chrome binary. Here's how you can do it:

    module.exports = function(config) {
      config.set({
        // ...
    
        browsers: ['ChromeHeadless'],
    
        customLaunchers: {
          ChromeHeadlessCustom: {
            base: 'ChromeHeadless',
            flags: ['--no-sandbox', '--disable-gpu', '--headless', '--disable-dev-shm-usage', '--remote-debugging-port=9222', '--disable-software-rasterizer'],
            binary: '/path/to/chrome/binary' // Specify the path to your Chrome/Chromium binary
          }
        },
      });
    };
    

    Replace /path/to/chrome/binary with the actual path to your Chrome or Chromium binary.

    Check Environment Variables: Ensure that there are no environment variables that could be affecting the ChromeHeadless execution. Sometimes, environment variables like CHROME_BIN can interfere with Karma's ability to locate the Chrome binary. You might need to unset these variables.

    Update Karma and Plugins: Ensure that Karma and the Karma plugins you are using are up to date. Outdated versions may have compatibility issues.