From 1d66d9658c4eb623c47f11c2426beccc5d4b0bc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20M=C3=A9tais?= Date: Wed, 3 Sep 2014 09:06:35 +0200 Subject: [PATCH] First server tests --- .gitignore | 3 +- Gruntfile.js | 47 +++++++++++++++++++++++++- package.json | 8 ++++- test/blanket.js | 7 ++++ test/server/phantomasWrapperTest.js | 52 +++++++++++++++++++++++++++++ test/server/testQueueTest.js | 11 ++++++ 6 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 test/blanket.js create mode 100644 test/server/phantomasWrapperTest.js create mode 100644 test/server/testQueueTest.js diff --git a/.gitignore b/.gitignore index a4eed78..8d078e8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules bower_components .vagrant -results/* \ No newline at end of file +results/* +coverage \ No newline at end of file diff --git a/Gruntfile.js b/Gruntfile.js index 7bc217c..3afa0f6 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -12,12 +12,57 @@ module.exports = function(grunt) { 'app/public/scripts/*.js', 'phantomas_custom/**/*.js' ] + }, + clean: { + coverage: { + src: ['coverage/'] + } + }, + copy: { + coverage: { + src: ['test/**'], + dest: 'coverage/' + } + }, + blanket: { + coverage: { + src: ['app/'], + dest: 'coverage/app/' + } + }, + mochaTest: { + test: { + options: { + reporter: 'spec', + }, + src: ['coverage/test/server/*.js'] + }, + coverage: { + options: { + reporter: 'html-cov', + // use the quiet flag to suppress the mocha console output + quiet: true, + // specify a destination file to capture the mocha + // output (the quiet option does not suppress this) + captureFile: 'coverage/coverage.html' + }, + src: ['coverage/test/server/*.js'] + } } }); require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks); - grunt.registerTask('hint', ['jshint']); + grunt.registerTask('hint', [ + 'jshint' + ]); + + grunt.registerTask('test', [ + 'clean:coverage', + 'blanket', + 'copy:coverage', + 'mochaTest' + ]); }; \ No newline at end of file diff --git a/package.json b/package.json index d14b337..18c33f9 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,13 @@ "devDependencies": { "grunt": "^0.4.5", "grunt-contrib-jshint": "^0.10.0", - "matchdep": "^0.3.0" + "matchdep": "^0.3.0", + "grunt-mocha-test": "^0.11.0", + "grunt-contrib-clean": "^0.6.0", + "grunt-contrib-copy": "^0.5.0", + "grunt-blanket": "0.0.8", + "chai": "^1.9.1", + "mocha": "^1.21.4" }, "scripts": {} } diff --git a/test/blanket.js b/test/blanket.js new file mode 100644 index 0000000..d8260ab --- /dev/null +++ b/test/blanket.js @@ -0,0 +1,7 @@ +var path = require('path'); +var srcDir = path.join(__dirname, '..', 'server'); + +require('blanket')({ + // Only files that match the pattern will be instrumented + pattern: srcDir +}); \ No newline at end of file diff --git a/test/server/phantomasWrapperTest.js b/test/server/phantomasWrapperTest.js new file mode 100644 index 0000000..8010f38 --- /dev/null +++ b/test/server/phantomasWrapperTest.js @@ -0,0 +1,52 @@ +var should = require('chai').should(); +var phantomasWrapper = require('../../app/lib/phantomasWrapper.js'); + +describe('phantomasWrapper', function() { + + it('should have a method execute', function() { + phantomasWrapper.should.have.property('execute').that.is.a('function'); + }); + + it('should execute', function(done) { + var url = 'http://www.google.fr'; + + this.timeout(15000); + phantomasWrapper.execute({ + url: url, + testId: '123abc', + options:{ + + } + }, function(err, json, results) { + should.not.exist(err); + + json.should.be.an('object'); + json.should.have.a.property('generator').that.contains('phantomas'); + json.should.have.a.property('url').that.equals(url); + json.should.have.a.property('metrics').that.is.an('object').not.empty(); + json.should.have.a.property('offenders').that.is.an('object').not.empty(); + json.offenders.should.have.a.property('javascriptExecutionTree').that.is.a('string'); + + done(); + }); + }); + + it('should fail with error 254', function(done) { + var url = 'http://www.not.existing'; + + this.timeout(15000); + phantomasWrapper.execute({ + url: url, + testId: '123abc', + options:{ + + } + }, function(err, json, results) { + should.exist(err); + err.should.equal(254); + should.not.exist(json); + + done(); + }); + }); +}); \ No newline at end of file diff --git a/test/server/testQueueTest.js b/test/server/testQueueTest.js new file mode 100644 index 0000000..47bce4f --- /dev/null +++ b/test/server/testQueueTest.js @@ -0,0 +1,11 @@ +var should = require('chai').should(); +var testQueue = require('../../app/lib/testQueue.js'); + +describe('testQueue', function() { + + it('should have a method push', function() { + testQueue.should.have.property('push').that.is.a('function'); + + }); + +}); \ No newline at end of file