DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Running JavaScript Tests On a CI Server with Karma, Chrome and Fake X

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

Jakub Holý user avatar by
Jakub Holý
·
Jan. 14, 15 · Java Zone · Interview
Like (1)
Save
Tweet
7.66K Views

Join the DZone community and get the full member experience.

Join For Free

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


CI/CD Testing JavaScript

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Building a QR Code Generator with Azure Functions
  • 5 Ways to Optimize Your CQL Queries for Performance
  • When Disaster Strikes: Production Troubleshooting
  • C++ Creator Bjarne Stroustrup Interview

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo