Browse Source

The test itself starts the server in a before hook

Gaël Métais 10 years ago
parent
commit
8abfe93df7
4 changed files with 25 additions and 24 deletions
  1. 3 11
      Gruntfile.js
  2. 11 10
      bin/server.js
  3. 10 2
      test/api/apiTest.js
  4. 1 1
      test/fixtures/settings.json

+ 3 - 11
Gruntfile.js

@@ -108,12 +108,6 @@ module.exports = function(grunt) {
                     showStack: true
                 }
             },
-            testServer: {
-                options: {
-                    port: 8387,
-                    server: './coverage/bin/server.js'
-                }
-            },
             testSuite: {
                 options: {
                     port: 8388,
@@ -148,7 +142,7 @@ module.exports = function(grunt) {
 
         var outputFile = './coverage/server_config/settings.json';
         grunt.file.write(outputFile, JSON.stringify(testSettings, null, 4));
-        grunt.verbose.ok('File ' + outputFile + ' created');
+        grunt.log.ok('File ' + outputFile + ' created');
     });
 
 
@@ -177,10 +171,9 @@ module.exports = function(grunt) {
     grunt.registerTask('test', [
         'build',
         'jshint',
-        'copy-test-server-settings',
-        'express:testServer',
         'express:testSuite',
         'clean:coverage',
+        'copy-test-server-settings',
         'blanket',
         'copy:coverage',
         'mochaTest:test',
@@ -190,10 +183,9 @@ module.exports = function(grunt) {
     grunt.registerTask('test-current-work', [
         'build',
         'jshint',
-        'copy-test-server-settings',
-        'express:testServer',
         'express:testSuite',
         'clean:coverage',
+        'copy-test-server-settings',
         'blanket',
         'copy:coverage',
         'mochaTest:test-current-work'

+ 11 - 10
bin/server.js

@@ -20,13 +20,14 @@ var uiController            = require('../lib/server/controllers/uiController')(
 
 
 // Let's start the server!
-if (!process.env.GRUNTED) {
-    // The server is not launched by Grunt
-    var settings = require('../server_config/settings.json');
-    server.listen(settings.serverPort, function() {
-        console.log('Listening on port %d', server.address().port);
-    });
-}
-
-// For Grunt
-module.exports = app;
+var settings = require('../server_config/settings.json');
+server.listen(settings.serverPort, function() {
+    console.log('Listening on port %d', server.address().port);
+
+    // For the tests
+    if (server.startTests) {
+        server.startTests();
+    }
+});
+
+module.exports = server;

+ 10 - 2
test/api/apiTest.js

@@ -14,7 +14,12 @@ var wwwUrl = 'http://localhost:8388';
 describe('api', function() {
 
     var runId;
+    var apiServer;
 
+    before(function(done) {
+        apiServer = require('../../bin/server.js');
+        apiServer.startTests = done;
+    });
 
     it('should refuse a query with an invalid key', function(done) {
         this.timeout(5000);
@@ -67,7 +72,7 @@ describe('api', function() {
     });
 
     it('should accept up to 10 anonymous runs to the API', function(done) {
-        this.timeout(15000);
+        this.timeout(5000);
 
         function launchRun() {
             var deferred = Q.defer();
@@ -126,5 +131,8 @@ describe('api', function() {
         
     });
 
-
+    after(function() {
+        console.log('Closing the server');
+        apiServer.close();
+    });
 });

+ 1 - 1
test/fixtures/settings.json

@@ -1,5 +1,5 @@
 {
-    "serverPort": "auto",
+    "serverPort": "8387",
     "googleAnalyticsId": "",
     
     "authorizedKeys": {