public
Last active

  • Download Gist
default.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
function runSpecs() {
// configure the spec runner
var specRunner = new Hilo.SpecRunner({
src: "Hilo",
specs: "specs",
helpers: "specs/Helpers"
});
 
// Handle any errors in the execution that
// were not part of a failing test
specRunner.addEventListener("error", function (args) {
document.querySelector("body").innerText = args.detail;
});
 
// run the specs
specRunner.run();
}
 
// ===============================================================================
// Microsoft patterns & practices
// Hilo JS Guidance
// ===============================================================================
// Copyright © Microsoft Corporation. All rights reserved.
// This code released under the terms of the
// Microsoft patterns & practices license (http://hilojs.codeplex.com/license)
// ===============================================================================
specRunner.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
(function (global) {
"use strict";
 
// SpecRunner Constructor
// ----------------------
 
// This objects searches through the project folder, the specs folder,
// and the specs helpers folder to find all available JavaScript files.
// It inserts a script tag in to the DOM for each file it finds.
//
// The `options` parameter allows three options to be passed in:
// * `specs`: the folder to search for `*spec.js` files
// * `helpers`: a folder that contains helper objects and methods for the specs
// * `src`: the folder that contains all fo the source files that will be tested
 
function SpecRunner(options) {
this.specFolder = options.specs || "specs";
this.helperFolder = options.helpers || "specs/helpers";
this.srcFolder = options.src || "src";
}
 
// SpecRunner Methods
// ------------------
 
var specRunnerMethods = {
configureMocha: function(){
global.expect = chai.expect;
global.mocha.setup("bdd");
},
 
run: function () {
this.appFolder = Windows.ApplicationModel.Package.current.installedLocation;
this.configureMocha();
 
this.injectHelpers()
.then(this.injectPageControls.bind(this))
.then(this.injectSpecList.bind(this))
.then(this.startTestHarness.bind(this))
.done(null, this.triggerError.bind(this));
},
 
triggerError: function (error) {
this.dispatchEvent("error", error);
},
 
startTestHarness: function () {
global.mocha.run();
},
 
injectPageControls: function () {
return this.getFolder(this.srcFolder)
.then(this.getJSFileNames.bind(this))
.then(this.buildScriptTags.bind(this))
.then(this.addScriptsToBody.bind(this));
},
 
injectHelpers: function () {
return this.getFolder(this.helperFolder)
.then(this.getJSFileNames.bind(this))
.then(this.buildScriptTags.bind(this))
.then(this.addScriptsToBody.bind(this));
},
 
injectSpecList: function () {
return this.getFolder(this.specFolder)
.then(this.getSpecFileNames.bind(this))
.then(this.buildScriptTags.bind(this))
.then(this.addScriptsToBody.bind(this));
},
 
getFolder: function (folderName, parentFolder) {
parentFolder = parentFolder || this.appFolder;
 
var names = folderName.split("/");
var name = names.shift();
 
var folder = parentFolder.getFolderAsync(name);
if (names.length === 0) {
 
// Found the final folder. Return it.
return folder;
 
} else {
 
// More folders to find. Recursively load them.
var that = this;
return folder.then(function (newParent) {
return that.getFolder(names.join("/"), newParent);
});
 
}
},
 
getJSFileNames: function (folder) {
var nameTest = /.*js$/;
return this._buildFileListFromRegex(nameTest, folder);
},
 
getSpecFileNames: function (folder) {
var specTest = /[Ss][Pp][Ee][Cc].*js/;
return this._buildFileListFromRegex(specTest, folder);
},
 
buildScriptTags: function (fileList) {
var appPath = this.appFolder.path;
 
var specList = fileList.map(function (file) {
var filePath = file.path.replace(appPath, "");
var scriptEl = document.createElement("script");
scriptEl.setAttribute("src", filePath);
 
return scriptEl;
});
 
return WinJS.Promise.as(specList);
},
 
addScriptsToBody: function (scriptTags) {
var body = document.querySelector("body");
scriptTags.forEach(function (tag) {
body.appendChild(tag);
});
 
return WinJS.Promise.as(true);
},
 
_buildFileListFromRegex: function(regEx, folder){
var fileQuery = folder.getFilesAsync(Windows.Storage.Search.CommonFileQuery.orderByName);
 
return fileQuery.then(function (files) {
var fileList = files.filter(function (file) {
return regEx.test(file.name);
});
return WinJS.Promise.as(fileList);
});
}
};
 
// Public API
// ----------
 
WinJS.Namespace.define("Hilo", {
SpecRunner: WinJS.Class.mix(SpecRunner, specRunnerMethods, WinJS.Utilities.eventMixin)
});
})(this);
 
// ===============================================================================
// Microsoft patterns & practices
// Hilo JS Guidance
// ===============================================================================
// Copyright © Microsoft Corporation. All rights reserved.
// This code released under the terms of the
// Microsoft patterns & practices license (http://hilojs.codeplex.com/license)
// ===============================================================================

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.