Running JavaScript Tests On a CI Server With Karma, Chrome And Fake X

So I want to run my JavaScript tests in a browser on our CI server. But the server has no graphical environment and the tests do not run under PhantomJS 1.x because it uses too old WebKit without ES5. The solution? Use a real browser and fake X via Xvfb. The browser I use is Chrome though Firefox would like work as well.

In code:


$ sudo apt-get install xvfb chromium-browser
$ test -e /tmp/.X99-lock || sudo /usr/bin/Xvfb :99 &
### Inside my app dir:
$ export DISPLAY=:99.0
$ export CHROME_BIN=/usr/bin/chromium-browser
$ npm install # this will also fire my Karma/Mocha tests


Notice that we start the fake X, tell Chrome that it should use its display via exporting DISPLAY (screen .0 is the default but I could have explicitely started Xvfb with f.x. "Xvfb :99 -screen 0 1024x768x24 ...") and use the <BROWSER>_BIN ENV variable to tell the karma-chrome-launcher to use /usr/bin/chromium-browser  instead of the default google-chrome.

Last but not least, we need to run Chrome without sandbox (at least on my server it failed with "PID namespaces supported Network namespace supported but failed: errno = Operation not permitted." This is done via a custom luncher in karma.conf.js:


module.exports = function(config) {
  config.set({
    browsers: ['Chrome'], // Note: PhantomJS does not work due to pre-ES5
    customLaunchers: {
      Chrome_without_sandbox: {
        base: 'Chrome',
        flags: ['--no-sandbox'] // with sandbox it fails under Docker
      }
    },
    frameworks: ['mocha'],
    files: ['app/w/dist/app-test.js']
  });
};


Finally, here are some relevant packages from package.json:


    &quot;karma&quot;: &quot;~0.12&quot;,
    &quot;karma-mocha&quot;: &quot;~0.1&quot;,
    &quot;karma-chrome-launcher&quot;: &quot;~0.1&quot;,
    &quot;mocha&quot;: &quot;~2.1.0&quot;,

Tags: JavaScript


Copyright © 2024 Jakub Holý
Powered by Cryogen
Theme by KingMob