Skip to content

Instantly share code, notes, and snippets.

@dylants
Created March 26, 2014 21:50
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dylants/9794431 to your computer and use it in GitHub Desktop.
Save dylants/9794431 to your computer and use it in GitHub Desktop.
Mocha and Jenkins integration for Titanium (Appcelerator). The titanium-tests.js contains the setup for Jenkins integration with ti-mocha (Mocha integrated into Titanium). The Gruntfile.js contains a way of monitoring for the done.testing file, which will kill the iOS simulator and allow the Jenkins build to complete.
/* global module:true */
module.exports = function(grunt) {
"use strict";
grunt.initConfig({
watch: {
files: "done.testing",
tasks: ["shell"],
options: {
event: ["added"]
}
},
shell: {
multiple: {
command: [
'killall "iPhone Simulator"',
"rm -f done.testing"
].join(";")
}
}
});
grunt.loadNpmTasks("grunt-contrib-watch");
grunt.loadNpmTasks('grunt-shell');
grunt.registerTask("default", ["watch"]);
};
/**
* Sets up Mocha to output the reporting in a way that Jenkins can understand
* it. This does so by sending the output to a file on /tmp/myProject named
* "test-reports.xml". We're using /tmp here because Titanium doesn't have an
* understanding of the current working directory (as far as I can tell). The
* idea is that /tmp/myProject is symlinked to the current directory "myProject".
*/
function setupJenkinsReporting() {
var outputFile;
// create the output file to send output to
outputFile = Titanium.Filesystem.getFile("/tmp/myProject", "test-reports.xml");
// if it already exists, clear it out and recreate it
if (outputFile.exists()) {
outputFile.deleteFile();
}
outputFile.createFile();
// write test output to Jenkins style output file
mocha.setup({
reporter: "xunit",
outputFile: outputFile
});
}
/**
* Listens for the "end" event on the runner to notify our Jenkins build
* that the testing is done, and it should shutdown the simulator and collect
* the test results. Jenkins is notified when this function creates a file,
* which another process is watching, called "done.testing". The creation of
* this file is what triggers the Jenkins build to kill the simulator and end
* the testing.
*
* @param {Object} runner The mocha runner
*/
function reportTestingDoneForJenkins(runner) {
// listen when the tests are complete
runner.on("end", function() {
console.log("Done running tests");
// Create a file to signal that the tests are complete
Titanium.Filesystem.getFile("/tmp/myProject", "done.testing").createFile();
});
}
///////////////////////////////////////
// SET TO TRUE IF RUNNING ON JENKINS //
///////////////////////////////////////
var IS_JENKINS = true;
// If not production, run the tests
if (Ti.App.deployType !== "production") {
var runner;
require("ti-mocha");
// create the test suite
describe("My Test Suite", function() {
// .... tests ....
});
// setup Jenkins reporting before running the tests (if necessary)
if (IS_JENKINS) {
setupJenkinsReporting();
}
// run the tests
runner = mocha.run();
// allow Jenkins to report testing is done (if necessary)
if (IS_JENKINS) {
reportTestingDoneForJenkins(runner);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment