Last active
October 25, 2016 08:50
-
-
Save d1b1/7949308 to your computer and use it in GitHub Desktop.
Programmatic Mocha script for CodeShip.io - Starts an express app, and runs any .js tests in the tests folder. This allows a node.js Swagger.js rest API to be testable on travis-ci and codeship.io.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Programmatic Mocha code - This is used by a codeship | |
test to both start an expressjs app, setup routes and | |
run a suite of mocha tests. | |
Hint: Place this in the root of the project. Call from | |
test package.js or setup with > node mocha.js | |
Also see: https://gist.github.com/d1b1/7949456 (Expressjs + Swagger App) | |
*/ | |
/* Dependencies */ | |
var Mocha = require('mocha'), | |
fs = require('fs'), | |
path = require('path') | |
/* Call Express App */ | |
var app = require('./test/lib/app') | |
/* Setup the Mocha for running the spec test. */ | |
var mocha = new Mocha({ | |
reporter: 'spec' | |
}) | |
/* Loop the test folder and load all the | |
.js test files into mocha. Assumes all | |
tests in the ./test folder. | |
*/ | |
fs.readdirSync('test').filter(function(file) { | |
// Only keep the .js files | |
return file.substr(-3) === '.js' | |
}).forEach(function(file){ | |
// Use the method "addFile" to add the file to mocha | |
mocha.addFile( | |
path.join('test', file) | |
) | |
}) | |
/* Start the Server on a port. once it is started, tell mocha to run. */ | |
Server = app.listen(3000, function (err, result) { | |
if (err) { | |
console.log('Failed') | |
process.exit(1) | |
} else { | |
// Now, you can run the tests. | |
mocha.run(function(failures) { | |
// Output the errors into the console for testing purposes. | |
console.log('Failures', failures) | |
// Exit the script with either 0 or the number of failures. | |
// Travis and Codeship will look for this value to determine | |
// if the tests passed. | |
process.exit(failures) | |
}) | |
} | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment