• 23-Jan-2023
Lightrun Team
Author Lightrun Team
Share
This article is about fixing ChromeHeadless can't run as root with --no-sandbox anymore in Karma Runner Karma

ChromeHeadless can’t run as root with –no-sandbox anymore in Karma Runner Karma

Lightrun Team
Lightrun Team
23-Jan-2023

Explanation of the problem

After updating to Chrome 62 and Chromedriver 2.33, an error is encountered when running Karma tests with ChromeHeadless. The error message is as follows:

25 10 2017 08:47:40.197:ERROR [launcher]: Cannot start ChromeHeadless
[1025/084740.006078:ERROR:zygote_host_impl_linux.cc(88)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.

This issue is observed in a Docker environment running Ubuntu 16.

The error message suggests that ChromeHeadless cannot be started due to a compatibility issue with the version of Chromedriver being used. Specifically, the error states that running Chrome as root without the “–no-sandbox” flag is not supported, as detailed in the link provided in the error message (https://crbug.com/638180).

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for ChromeHeadless can’t run as root with –no-sandbox anymore in Karma Runner Karma

The issue at hand when running Chrome headless in a sandboxed environment is that certain features and behaviors may not work as expected. This is because the sandbox is a security feature that isolates the browser process from the rest of the system, and some tests may require access to system resources that are not available in the sandbox.

One solution to this issue is to disable the sandbox when running headless tests. This can be done by using a custom launcher in your test runner, as shown in the first answer. This is a configuration that tells the test runner to use a specific browser, in this case ChromeHeadlessNoSandbox, and this browser is specified as a custom launcher in the customLauncher object with the flags property set to ‘–no-sandbox’, this configuration tells the browser to run without the sandbox, it’s important to keep in mind that disabling the sandbox can expose the system to potential security risks, so use it with caution and only if needed.

Another solution is to use the “puppeteer” library and launch the browser with the “–no-sandbox” flag as shown in the second answer. Puppeteer is a Node library that provides a high-level API to control headless Chrome or Chromium over the DevTools Protocol.

const puppeteer = require('puppeteer');
const browser = await puppeteer.launch({headless: true, args:['--no-sandbox']});

This way you can access the browser instance via the “browser” variable and run it without a sandbox and you can access the browser instance via the “browser” variable. However, keep in mind that disabling the sandbox can expose the system to potential security risks, so use it with caution and only if needed.

Other popular problems with Karma

Problem: Tests are not being run

One of the most common problems with Karma is that tests are not being run. This can happen for various reasons, such as a misconfigured file pattern, missing dependencies, or a syntax error in the test code.

Solution:

To solve this problem, you should first check the Karma configuration file (karma.conf.js) and make sure that the file patterns for the test files and the source files are correct. Also, check that all the required dependencies are installed and imported correctly.

//example of karma.conf.js
module.exports = function(config) {
  config.set({
    files: [
      'src/**/*.js', //this is the pattern for source files
      'test/**/*.spec.js' //this is the pattern for test files
    ],
    frameworks: ['jasmine'],
    preprocessors: {
      'src/**/*.js': ['babel'],
      'test/**/*.spec.js': ['babel']
    },
    //...
  });
};

Problem: Tests are failing with a timeout

Another common problem with Karma is that tests are failing with a timeout error. This can happen when tests are taking too long to complete or when there is a problem with the test runner’s communication with the browser.

Solution:

To solve this problem, you can increase the timeout value in the Karma configuration file or you can fix the problem by looking into the browser’s console for error messages and fix them.

//example of karma.conf.js
module.exports = function(config) {
  config.set({
    //...
    browserNoActivityTimeout: 60000, //increase the timeout value to 60 seconds
    //...
  });
};

Problem: Tests are failing with a “Cannot read property ‘xxx’ of undefined” error

Another common problem with Karma is that tests are failing with a “Cannot read property ‘xxx’ of undefined” error. This error happens when the test is trying to access a property of an object that is undefined. This can happen when an object is not defined or when a module is not imported correctly.

Solution:

To solve this problem, you should check that all the required modules are imported correctly and that objects are defined before they are used in the test.

//example of test file
import { MyObject } from './myObject';

describe('MyObject', () => {
  it('should have a property x', () => {
    const obj = new MyObject();
    expect(obj.x).toBeDefined();
  });
});
//example of myObject.js
export class MyObject {
  constructor() {
    this.x = 'some value';
  }
}

In these examples, the first problem is solved by checking that the file patterns for the test files and the source files are correct and that all the required dependencies are installed and imported correctly. The second problem is solved by increasing the timeout value in the Karma configuration file. The third problem is solved by checking that all the required modules are imported correctly and that objects are defined before they are used in the test.

A brief introduction to Karma

Karma is a test runner for JavaScript that can be used to run tests for web applications. It is designed to work with a variety of testing frameworks, such as Jasmine, Mocha, and Protractor. Karma uses a browser-based test runner to execute tests, which allows you to run tests in multiple browsers and on different platforms. This allows you to test your web application in a variety of environments and ensure that it works as expected.

Karma is highly configurable and can be integrated with a variety of build tools, such as Grunt, Gulp, and Webpack. It also has a plugin system that allows you to add additional functionality, such as code coverage reporting, test reporting, and test watch mode. Karma also provides an API that allows you to interact with the test runner programmatically, which can be useful for automating testing tasks. Overall, Karma is a powerful tool that can help you test your web application and ensure that it is working correctly.

Most popular use cases for Karma

  1. Karma can be used to run unit tests for JavaScript code. Unit tests are small, isolated tests that check the functionality of individual units of code, such as functions or objects. Karma can be configured to run these tests in multiple browsers and on different platforms, which allows you to test your code in a variety of environments and ensure that it works as expected.
describe('MyFunction', () => {
  it('should return the correct value', () => {
    expect(MyFunction()).toEqual('some value');
  });
});
  1. Karma can be used to run end-to-end tests for web applications. End-to-end tests are tests that simulate the behavior of a user interacting with a web application. Karma can be configured to run these tests in multiple browsers and on different platforms, which allows you to test your web application in a variety of environments and ensure that it works as expected.
  2. Karma can be used for Automated testing for the Continuous Integration (CI) process. Karma can be integrated with popular CI tools like Jenkins, TravisCI, and CircleCI and can be run as part of the build pipeline to check the application’s functionality in a consistent and repeatable way. This helps in catching errors early in the development cycle and improves the overall application quality.
Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications. It’s a registration form away.

Get Lightrun

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.