DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
What's in store for DevOps in 2023? Hear from the experts in our "DZone 2023 Preview: DevOps Edition" on Fri, Jan 27!
Save your seat
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. 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 · Interview
Like (1)
Save
Tweet
Share
7.81K 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",


Continuous Integration/Deployment 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

  • Beginners’ Guide to Run a Linux Server Securely
  • Handling Virtual Threads
  • 2023 Software Testing Trends: A Look Ahead at the Industry's Future
  • How to Configure AWS Glue Job Using Python-Based AWS CDK

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: