From b26c8c3e74d15cddee64b15a09f25df1241043fc Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Fri, 16 Oct 2015 21:43:43 -0500 Subject: [PATCH] Add api test task --- tasks/gulp-tests.js | 47 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/tasks/gulp-tests.js b/tasks/gulp-tests.js index ef1bfcbd17..50e70a4ecd 100644 --- a/tasks/gulp-tests.js +++ b/tasks/gulp-tests.js @@ -4,21 +4,24 @@ import mongoose from 'mongoose'; import { exec } from 'child_process'; import psTree from 'ps-tree'; import gulp from 'gulp'; +import {sync as glob} from 'glob'; import Q from 'q'; +import Mocha from 'mocha'; const TEST_SERVER_PORT = 3001 const TEST_DB = 'habitrpg_test' const TEST_DB_URI = `mongodb://localhost/${TEST_DB}` -const API_TEST_COMMAND = 'mocha test/api-legacy'; +const API_TEST_COMMAND = 'mocha test/api'; +const LEGACY_API_TEST_COMMAND = 'mocha test/api-legacy'; const COMMON_TEST_COMMAND = 'mocha test/common'; const CONTENT_TEST_COMMAND = 'mocha test/content --opts test/content/mocha.content.opts'; const CONTENT_OPTIONS = {maxBuffer: 1024 * 500}; const KARMA_TEST_COMMAND = 'karma start'; const SERVER_SIDE_TEST_COMMAND = 'mocha test/server_side'; -const ISTANBUL_TEST_COMMAND = `istanbul cover -i "website/src/**" --dir coverage/api $(npm bin)/${API_TEST_COMMAND}`; +const ISTANBUL_TEST_COMMAND = `istanbul cover -i "website/src/**" --dir coverage/api $(npm bin)/${LEGACY_API_TEST_COMMAND}`; /* Helper methods for reporting test summary */ let testResults = []; @@ -176,7 +179,7 @@ gulp.task('test:api-legacy:safe', ['test:prepare:mongo'], (cb) => { }); gulp.task('test:api-legacy:clean', (cb) => { - pipe(exec(testBin(API_TEST_COMMAND), () => cb())); + pipe(exec(testBin(LEGACY_API_TEST_COMMAND), () => cb())); }); gulp.task('test:api-legacy:watch', [ @@ -279,10 +282,40 @@ gulp.task('test:e2e:safe', ['test:prepare'], (cb) => { }); }); +gulp.task('test:api', ['test:startServer', 'test:prepare:mongo'], (done) => { + let mocha = new Mocha(); + let tests = glob('./test/api/**/*.js'); + + tests.forEach((test) => { + delete require.cache[resolve(test)]; + mocha.addFile(test); + }); + + mocha.run((numberOfFailures) => { + done(); + }); +}); + +gulp.task('test:api:safe', ['test:startServer', 'test:prepare:mongo'], (done) => { + let runner = exec( + testBin(API_TEST_COMMAND), + (err, stdout, stderr) => { + testResults.push({ + suite: 'API Specs', + pass: testCount(stdout, /(\d+) passing/), + fail: testCount(stderr, /(\d+) failing/), + pend: testCount(stdout, /(\d+) pending/) + }); + done(); + } + ); + pipe(runner); +}); + gulp.task('test:startServer', (done) => { - process.env.NODE_DB_URI = TEST_DB_URI; + process.env.NODE_DB_URI = `mongodb://localhost/${TEST_DB}-api`; process.env.DISABLE_REQUEST_LOGGING = true; - process.env.PORT = TEST_SERVER_PORT; + process.env.PORT = 3002; let server = require('../website/src/server.js'); server.listen(TEST_SERVER_PORT, done); @@ -293,7 +326,8 @@ gulp.task('test', [ 'test:content:safe', 'test:server_side:safe', 'test:karma:safe', - 'test:api-legacy:safe', + // 'test:api-legacy:safe', + 'test:api:safe', 'test:e2e:safe' ], () => { let totals = [0,0,0]; @@ -321,5 +355,6 @@ gulp.task('test', [ if (totals[1] > 0) throw "ERROR: There are failing tests!" else { console.log('\n\x1b[36mThanks for helping keep Habitica clean!\x1b[0m'); + process.exit(); } });