From cceb8a3c26b9e1040a81cf90eb79adf07c4bf8ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 30 Sep 2019 09:14:35 +0200 Subject: [PATCH 1/3] test: switch from PhantomJS to HeadlessChrome MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rework browser tests to run in Headless Chrome instead of PhantomJS, because the latter is no longer maintained. This allows us to remove transpilation to ES5 via babelify, which significantly improves speed of our tests. Signed-off-by: Miroslav Bajtoš --- .travis.yml | 14 +++----------- Gruntfile.js | 12 ++---------- package.json | 5 +---- test/karma.conf.js | 34 ++++++++++++---------------------- 4 files changed, 18 insertions(+), 47 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2b9bf173..806e70ad 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,18 +5,10 @@ node_js: - "10" - "12" +addons: + chrome: stable + after_success: npm run coverage -# see https://www.npmjs.com/package/phantomjs-prebuilt#continuous-integration -cache: - directories: - - travis_phantomjs before_install: - npm config set registry http://ci.strongloop.com:4873/ - # Upgrade PhantomJS to v2.1.1. - - "export PHANTOMJS_VERSION=2.1.1" - - "export PATH=$PWD/travis_phantomjs/phantomjs-$PHANTOMJS_VERSION-linux-x86_64/bin:$PATH" - - "if [ $(phantomjs --version) != $PHANTOMJS_VERSION ]; then rm -rf $PWD/travis_phantomjs; mkdir -p $PWD/travis_phantomjs; fi" - - "if [ $(phantomjs --version) != $PHANTOMJS_VERSION ]; then wget https://github.com/Medium/phantomjs/releases/download/v$PHANTOMJS_VERSION/phantomjs-$PHANTOMJS_VERSION-linux-x86_64.tar.bz2 -O $PWD/travis_phantomjs/phantomjs-$PHANTOMJS_VERSION-linux-x86_64.tar.bz2; fi" - - "if [ $(phantomjs --version) != $PHANTOMJS_VERSION ]; then tar -xvf $PWD/travis_phantomjs/phantomjs-$PHANTOMJS_VERSION-linux-x86_64.tar.bz2 -C $PWD/travis_phantomjs; fi" - - "phantomjs --version" diff --git a/Gruntfile.js b/Gruntfile.js index 01e33dc9..19671a44 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -31,11 +31,6 @@ module.exports = function(grunt) { }, }, }, - run: { - optionalInstall: { - exec: 'npm install --no-save --silent karma-phantomjs-launcher phantomjs-prebuilt', - }, - }, eslint: { gruntfile: { src: 'Gruntfile.js', @@ -109,7 +104,7 @@ module.exports = function(grunt) { karma: { 'unit-once': { configFile: 'test/karma.conf.js', - browsers: ['PhantomJS'], + browsers: ['ChromeHeadless'], singleRun: true, reporters: ['dots', 'junit'], @@ -216,7 +211,6 @@ module.exports = function(grunt) { }); // These plugins provide necessary tasks. - grunt.loadNpmTasks('grunt-run'); grunt.loadNpmTasks('grunt-browserify'); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-eslint'); @@ -242,14 +236,12 @@ module.exports = function(grunt) { // Default task. grunt.registerTask('default', ['browserify']); - grunt.registerTask('phantomTests', ['run', 'karma:unit-once']); - grunt.registerTask('test', [ 'eslint', process.env.JENKINS_HOME ? 'mochaTest:unit-xml' : 'mochaTest:unit', process.env.JENKINS_HOME && (/^win/.test(process.platform) || /^s390x/.test(process.arch) || /^ppc64/.test(process.arch)) ? - 'skip-karma' : 'phantomTests', + 'skip-karma' : 'karma:unit-once', ]); // alias for sl-ci-run and `npm test` diff --git a/package.json b/package.json index 3aea05ec..b6dbd7e1 100644 --- a/package.json +++ b/package.json @@ -63,9 +63,7 @@ "underscore.string": "^3.3.5" }, "devDependencies": { - "babel-preset-es2015": "^6.22.0", - "babelify": "^7.3.0", - "browserify": "^13.1.0", + "browserify": "^16.5.0", "chai": "^3.5.0", "cookie-parser": "^1.3.4", "coveralls": "^3.0.2", @@ -82,7 +80,6 @@ "grunt-eslint": "^21.0.0", "grunt-karma": "^3.0.2", "grunt-mocha-test": "^0.13.3", - "grunt-run": "^0.8.1", "karma": "^4.1.0", "karma-browserify": "^6.0.0", "karma-chrome-launcher": "^2.2.0", diff --git a/test/karma.conf.js b/test/karma.conf.js index 1180b0f5..1567fe20 100644 --- a/test/karma.conf.js +++ b/test/karma.conf.js @@ -64,7 +64,6 @@ module.exports = function(config) { 'karma-browserify', 'karma-es6-shim', 'karma-mocha', - 'karma-phantomjs-launcher', 'karma-chrome-launcher', 'karma-junit-reporter', ], @@ -104,27 +103,18 @@ module.exports = function(config) { 'superagent', 'supertest', ], - transform: [ - ['babelify', { - presets: [ - ['es2015', { - // Disable transform-es2015-modules-commonjs which adds - // "use strict" to all files, even those that don't work - // in strict mode - // (e.g. chai, loopback-datasource-juggler, etc.) - modules: false, - }], - ], - // By default, browserify does not transform node_modules - // As a result, our dependencies like strong-remoting and juggler - // are kept in original ES6 form that does not work in PhantomJS - global: true, - // Prevent SyntaxError in strong-task-emitter: - // strong-task-emitter/lib/task.js (83:4): - // arguments is a reserved word in strict mode - ignore: /node_modules\/(strong-task-emitter)\//, - }], - ], + packageFilter: function(pkg, dir) { + // async@3 (used e.g. by loopback-connector) is specifying custom + // browserify config, in particular it wants to apply transformation + // `babelify`. We don't have `babelify` installed because we are + // testing using latest Chrome and thus don't need any transpilation. + // Let's remove the browserify config from the package and force + // browserify to use our config instead. + if (pkg.name === 'async') { + delete pkg.browserify; + } + return pkg; + }, debug: true, // noParse: ['jquery'], watch: true, From 8311138f3e7151609a267bc4c74f0cd4528a00f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 30 Sep 2019 11:14:04 +0200 Subject: [PATCH 2/3] test: disable Chrome sandboxing when inside Docker MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See the discussion in https://github.com/docker/for-linux/issues/496 Signed-off-by: Miroslav Bajtoš --- Gruntfile.js | 2 +- package.json | 1 + test/karma.conf.js | 21 ++++++++++++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 19671a44..46ebf98b 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -104,7 +104,7 @@ module.exports = function(grunt) { karma: { 'unit-once': { configFile: 'test/karma.conf.js', - browsers: ['ChromeHeadless'], + browsers: ['ChromeDocker'], singleRun: true, reporters: ['dots', 'junit'], diff --git a/package.json b/package.json index b6dbd7e1..a98f9c80 100644 --- a/package.json +++ b/package.json @@ -80,6 +80,7 @@ "grunt-eslint": "^21.0.0", "grunt-karma": "^3.0.2", "grunt-mocha-test": "^0.13.3", + "is-docker": "^2.0.0", "karma": "^4.1.0", "karma-browserify": "^6.0.0", "karma-chrome-launcher": "^2.2.0", diff --git a/test/karma.conf.js b/test/karma.conf.js index 1567fe20..6b0c0fd5 100644 --- a/test/karma.conf.js +++ b/test/karma.conf.js @@ -3,12 +3,31 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT +'use strict'; + +const isDocker = require('is-docker'); + // Karma configuration // http://karma-runner.github.io/0.12/config/configuration-file.html -'use strict'; module.exports = function(config) { + // see https://github.com/docker/for-linux/issues/496 + const disableChromeSandbox = isDocker() && !process.env.TRAVIS; + if (disableChromeSandbox) { + console.log('!! Disabling Chrome sandbox to support un-privileged Docker !!'); + } + config.set({ + customLaunchers: { + ChromeDocker: { + base: 'ChromeHeadless', + // We must disable the Chrome sandbox when running Chrome inside Docker + // (Chrome's sandbox needs more permissions than Docker allows by default) + // See https://github.com/docker/for-linux/issues/496 + flags: disableChromeSandbox ? ['--no-sandbox'] : [], + }, + }, + // enable / disable watching file and executing tests whenever any file changes autoWatch: true, From 387835faeff01888a734de166892f8906ce09be6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Tue, 1 Oct 2019 08:12:52 +0200 Subject: [PATCH 3/3] test: use Chromium (not Chrome) when available MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miroslav Bajtoš --- package.json | 3 ++- test/karma.conf.js | 8 +++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index a98f9c80..f51d1c3b 100644 --- a/package.json +++ b/package.json @@ -98,7 +98,8 @@ "sinon-chai": "^3.2.0", "strong-error-handler": "^3.0.0", "strong-task-emitter": "^0.0.8", - "supertest": "^3.0.0" + "supertest": "^3.0.0", + "which": "^1.3.1" }, "repository": { "type": "git", diff --git a/test/karma.conf.js b/test/karma.conf.js index 6b0c0fd5..d3785a0d 100644 --- a/test/karma.conf.js +++ b/test/karma.conf.js @@ -6,6 +6,7 @@ 'use strict'; const isDocker = require('is-docker'); +const which = require('which'); // Karma configuration // http://karma-runner.github.io/0.12/config/configuration-file.html @@ -17,10 +18,15 @@ module.exports = function(config) { console.log('!! Disabling Chrome sandbox to support un-privileged Docker !!'); } + const hasChromium = + which.sync('chromium-browser', {nothrow: true}) || + which.sync('chromium', {nothrow: true}); + config.set({ customLaunchers: { ChromeDocker: { - base: 'ChromeHeadless', + // cis-jenkins build server does not provide Chrome, only Chromium + base: hasChromium ? 'ChromiumHeadless' : 'ChromeHeadless', // We must disable the Chrome sandbox when running Chrome inside Docker // (Chrome's sandbox needs more permissions than Docker allows by default) // See https://github.com/docker/for-linux/issues/496