Issues with Headless Chrome in Angular 5 Unit Testing on Ubuntu Server

I’m having trouble with my Angular 5 unit tests on an Ubuntu server. We use Karma and Jasmine for testing and run them with Headless Chrome for deployment purposes.

The tests work fine on my local machine but get stuck on the server. Here’s what I see in the logs:

05 03 2018 07:43:02.132:INFO [HeadlessChrome 64.0.3282 (Ubuntu 0.0.0)]: Connected on socket 9NdPfrccf8iYNlPrAAAA with id 56814881
05 03 2018 07:45:21.364:WARN [HeadlessChrome 64.0.3282 (Ubuntu 0.0.0)]: Disconnected (1 times)
HeadlessChrome 64.0.3282 (Ubuntu 0.0.0) ERROR
  Disconnectedundefined

Interestingly, the tests run without issues when using Firefox Headless. Has anyone encountered this problem or know how to fix it? Any help would be much appreciated!

I’ve encountered this issue before when setting up CI/CD pipelines. One thing that often gets overlooked is the memory allocation for Headless Chrome. On resource-constrained servers, Chrome can crash if it doesn’t have enough memory.

Try increasing the available memory for Chrome by adding the --disable-dev-shm-usage flag to your Chrome options in karma.conf.js. This forces Chrome to use /tmp instead of /dev/shm, which typically has more space.

Also, check if your tests are timing out. You might need to increase the timeout settings in both Karma and Jasmine configurations. Sometimes, slower server environments need more time to complete tests.

If these don’t work, consider using a tool like xvfb to create a virtual framebuffer. This can sometimes resolve issues with Headless Chrome on servers without a proper display environment.

I’ve faced a similar issue with Headless Chrome on Ubuntu servers. In my experience, it often boils down to missing dependencies or configuration problems. Here’s what worked for me:

First, make sure you have all necessary dependencies installed. Chrome needs certain libraries that might not be present on a minimal Ubuntu server. Try running:

sudo apt-get install -y gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget

Also, check your Karma configuration. Ensure you’re using the correct Chrome binary path and setting the necessary flags. In my karma.conf.js, I added:

browsers: [‘ChromeHeadlessNoSandbox’],
customLaunchers: {
ChromeHeadlessNoSandbox: {
base: ‘ChromeHeadless’,
flags: [‘–no-sandbox’, ‘–disable-gpu’, ‘–disable-dev-shm-usage’]
}
}

These tweaks resolved my issues. If problems persist, consider increasing the browser disconnect timeout in your Karma config.

hey mate, i had a similar headache. try adding --no-sandbox flag to ur chrome options in karma config. also, make sure ur using latest chrome version. if that doesnt work, u might need to tweak some system settings. good luck!