Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Running JavaScript Tests On a CI Server with Karma, Chrome and Fake X

DZone's Guide to

Running JavaScript Tests On a CI Server with Karma, Chrome and Fake X

· Java Zone
Free Resource

Build vs Buy a Data Quality Solution: Which is Best for You? Gain insights on a hybrid approach. Download white paper now!

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:

"karma": "~0.12",
"karma-mocha": "~0.1",
"karma-chrome-launcher": "~0.1",
"mocha": "~2.1.0",


Build vs Buy a Data Quality Solution: Which is Best for You? Maintaining high quality data is essential for operational efficiency, meaningful analytics and good long-term customer relationships. But, when dealing with multiple sources of data, data quality becomes complex, so you need to know when you should build a custom data quality tools effort over canned solutions. Download our whitepaper for more insights into a hybrid approach.

Topics:

Published at DZone with permission of Jakub Holý, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}