From 50816ebbe371589db93d1a08e68a218307b31153 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 23 Jun 2014 14:44:19 +0200 Subject: [PATCH 1/2] Fix loopback in PhantomJS, fix karma tests - Move configuration of Karma unit-tests from `Gruntfile.js` to a standalone file (`test/karma.conf.js`). - Add a new Grunt task `karma:unit-ci` to run Karma unit-tests in PhantomJS and produce karma-xunit.xml file that can be consumed by the CI server. - Add grunt-mocha-test, configure it to run unit-tests. - Add `grunt test` task that runs both karma and mocha tests, detects Jenkins to produce XML output on CI server. - Modify the `test` script in `package.json` to run `grunt mocha-and-karma` (an alias for `grunt test`). The alias is required to trick `sl-ci-run` to run `npm test` instead of calling directly `mocha`. - Add `es5-shim` module to karma unit-tests in order to provide ES5-methods required by LoopBack. - Fix `mixin(source)` in lib/loopback.js to work in PhantomJS. `Object.getOwnPropertyDescriptor()` provided by `es5-shim` does not work in the same way as in Node. --- .gitignore | 1 + Gruntfile.js | 116 +++++++++++++++------------------------------ lib/loopback.js | 4 ++ package.json | 36 +++++++------- test/karma.conf.js | 98 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 161 insertions(+), 94 deletions(-) create mode 100644 test/karma.conf.js diff --git a/.gitignore b/.gitignore index fdcae0c8..a30033bd 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ *.swo node_modules dist +*xunit.xml diff --git a/Gruntfile.js b/Gruntfile.js index 205feff8..09aa93fe 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -1,6 +1,8 @@ /*global module:false*/ module.exports = function(grunt) { + grunt.loadNpmTasks('grunt-mocha-test'); + // Project configuration. grunt.initConfig({ // Metadata. @@ -53,87 +55,39 @@ module.exports = function(grunt) { } } }, - karma: { - unit: { + mochaTest: { + 'unit': { + src: 'test/*.js', options: { - // base path, that will be used to resolve files and exclude - basePath: '', - - // frameworks to use - frameworks: ['mocha', 'browserify'], - - // list of files / patterns to load in the browser - files: [ - 'test/support.js', - 'test/model.test.js', - 'test/geo-point.test.js', - 'test/app.test.js' - ], - - // list of files to exclude - exclude: [ - - ], - - // test results reporter to use - // possible values: 'dots', 'progress', 'junit', 'growl', 'coverage' - reporters: ['dots'], - - // web server port - port: 9876, - - // cli runner port - runnerPort: 9100, - - // enable / disable colors in the output (reporters and logs) - colors: true, - - // level of logging - // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG - logLevel: 'warn', - - // enable / disable watching file and executing tests whenever any file changes - autoWatch: true, - - // Start these browsers, currently available: - // - Chrome - // - ChromeCanary - // - Firefox - // - Opera - // - Safari (only Mac) - // - PhantomJS - // - IE (only Windows) - browsers: [ - 'Chrome' - ], - - // If browser does not capture in given timeout [ms], kill it - captureTimeout: 60000, - - // Continuous Integration mode - // if true, it capture browsers, run tests and exit - singleRun: false, - - // Browserify config (all optional) - browserify: { - // extensions: ['.coffee'], - ignore: [ - 'nodemailer', - 'passport', - 'passport-local', - 'superagent', - 'supertest' - ], - // transform: ['coffeeify'], - // debug: true, - // noParse: ['jquery'], - watch: true, - }, - - // Add browserify to preprocessors - preprocessors: {'test/*': ['browserify']} + reporter: 'dot', } }, + 'unit-xml': { + src: 'test/*.js', + options: { + reporter: 'xunit', + captureFile: 'xunit.xml' + } + } + }, + karma: { + 'unit-once': { + configFile: 'test/karma.conf.js', + browsers: [ 'PhantomJS' ], + singleRun: true, + reporters: ['dots', 'junit'], + + // increase the timeout for slow build slaves (e.g. Travis-ci) + browserNoActivityTimeout: 30000, + + // CI friendly test output + junitReporter: { + outputFile: 'karma-xunit.xml' + }, + }, + unit: { + configFile: 'test/karma.conf.js', + }, e2e: { options: { // base path, that will be used to resolve files and exclude @@ -234,4 +188,10 @@ module.exports = function(grunt) { // Default task. grunt.registerTask('default', ['browserify']); + grunt.registerTask('test', [ + process.env.JENKINS_HOME ? 'mochaTest:unit-xml' : 'mochaTest:unit', + 'karma:unit-once']); + + // alias for sl-ci-run and `npm test` + grunt.registerTask('mocha-and-karma', ['test']); }; diff --git a/lib/loopback.js b/lib/loopback.js index 0f9f72f7..40eca19c 100644 --- a/lib/loopback.js +++ b/lib/loopback.js @@ -80,6 +80,10 @@ function createApplication() { function mixin(source) { for (var key in source) { var desc = Object.getOwnPropertyDescriptor(source, key); + + // Fix for legacy (pre-ES5) browsers like PhantomJS + if (!desc) continue; + Object.defineProperty(loopback, key, desc); } } diff --git a/package.json b/package.json index 0c7f820c..7cec2837 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ ], "version": "2.0.0-beta3", "scripts": { - "test": "mocha -R spec" + "test": "grunt mocha-and-karma" }, "dependencies": { "debug": "~0.8.1", @@ -49,31 +49,35 @@ "loopback-datasource-juggler": "~2.0.0-beta1" }, "devDependencies": { + "browserify": "~4.1.6", + "chai": "~1.9.1", "cookie-parser": "~1.1.0", "errorhandler": "~1.0.1", - "serve-favicon": "~2.0.0", - "loopback-datasource-juggler": "~2.0.0-beta1", - "mocha": "~1.20.1", - "strong-task-emitter": "0.0.x", - "supertest": "~0.13.0", - "chai": "~1.9.1", - "loopback-boot": "1.x >=1.1", - "loopback-testing": "~0.2.0", - "browserify": "~4.1.6", + "es5-shim": "^3.4.0", "grunt": "~0.4.5", "grunt-browserify": "~2.1.0", - "grunt-contrib-uglify": "~0.4.0", + "grunt-cli": "^0.1.13", "grunt-contrib-jshint": "~0.10.0", + "grunt-contrib-uglify": "~0.4.0", "grunt-contrib-watch": "~0.6.1", - "karma-script-launcher": "~0.1.0", + "grunt-karma": "~0.8.3", + "grunt-mocha-test": "^0.11.0", + "karma": "~0.12.16", + "karma-browserify": "~0.2.1", "karma-chrome-launcher": "~0.1.4", "karma-firefox-launcher": "~0.1.3", "karma-html2js-preprocessor": "~0.1.0", + "karma-junit-reporter": "^0.2.2", + "karma-mocha": "^0.1.4", "karma-phantomjs-launcher": "~0.1.4", - "karma": "~0.12.16", - "karma-browserify": "~0.2.1", - "karma-mocha": "~0.1.3", - "grunt-karma": "~0.8.3" + "karma-script-launcher": "~0.1.0", + "loopback-boot": "1.x >=1.1", + "loopback-datasource-juggler": "~2.0.0-beta1", + "loopback-testing": "~0.2.0", + "mocha": "~1.20.1", + "serve-favicon": "~2.0.0", + "strong-task-emitter": "0.0.x", + "supertest": "~0.13.0" }, "repository": { "type": "git", diff --git a/test/karma.conf.js b/test/karma.conf.js new file mode 100644 index 00000000..6b712904 --- /dev/null +++ b/test/karma.conf.js @@ -0,0 +1,98 @@ +// Karma configuration +// http://karma-runner.github.io/0.12/config/configuration-file.html + +module.exports = function(config) { + config.set({ + // enable / disable watching file and executing tests whenever any file changes + autoWatch: true, + + // base path, that will be used to resolve files and exclude + basePath: '../', + + // testing framework to use (jasmine/mocha/qunit/...) + frameworks: ['mocha', 'browserify'], + + // list of files / patterns to load in the browser + files: [ + 'node_modules/es5-shim/es5-shim.js', + 'test/support.js', + 'test/model.test.js', + 'test/geo-point.test.js', + 'test/app.test.js' + ], + + // list of files / patterns to exclude + exclude: [ + ], + + // test results reporter to use + // possible values: 'dots', 'progress', 'junit', 'growl', 'coverage' + reporters: ['dots'], + + // web server port + port: 9876, + + // cli runner port + runnerPort: 9100, + + // Start these browsers, currently available: + // - Chrome + // - ChromeCanary + // - Firefox + // - Opera + // - Safari (only Mac) + // - PhantomJS + // - IE (only Windows) + browsers: [ + 'Chrome' + ], + + // Which plugins to enable + plugins: [ + 'karma-browserify', + 'karma-mocha', + 'karma-phantomjs-launcher', + 'karma-chrome-launcher', + 'karma-junit-reporter' + ], + + // If browser does not capture in given timeout [ms], kill it + captureTimeout: 60000, + + // Continuous Integration mode + // if true, it capture browsers, run tests and exit + singleRun: false, + + colors: true, + + // level of logging + // possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG + logLevel: config.LOG_INFO, + + // Uncomment the following lines if you are using grunt's server to run the tests + // proxies: { + // '/': 'http://localhost:9000/' + // }, + // URL root prevent conflicts with the site root + // urlRoot: '_karma_' + + // Browserify config (all optional) + browserify: { + // extensions: ['.coffee'], + ignore: [ + 'nodemailer', + 'passport', + 'passport-local', + 'superagent', + 'supertest' + ], + // transform: ['coffeeify'], + debug: true, + // noParse: ['jquery'], + watch: true, + }, + + // Add browserify to preprocessors + preprocessors: {'test/*': ['browserify']} + }); +}; From fb66bf5c448dd2ab8ea61d6a86102dfceeed4de3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Tue, 24 Jun 2014 08:31:31 +0200 Subject: [PATCH 2/2] package: upgrade juggler to 2.0.0-beta2 --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 7cec2837..d6625d3e 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "canonical-json": "0.0.4" }, "peerDependencies": { - "loopback-datasource-juggler": "~2.0.0-beta1" + "loopback-datasource-juggler": "~2.0.0-beta2" }, "devDependencies": { "browserify": "~4.1.6", @@ -72,7 +72,7 @@ "karma-phantomjs-launcher": "~0.1.4", "karma-script-launcher": "~0.1.0", "loopback-boot": "1.x >=1.1", - "loopback-datasource-juggler": "~2.0.0-beta1", + "loopback-datasource-juggler": "~2.0.0-beta2", "loopback-testing": "~0.2.0", "mocha": "~1.20.1", "serve-favicon": "~2.0.0",