Over a million developers have joined DZone.

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

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

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) {
    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",

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.


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

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}