nativescript-unit-test-runner
Version:
NativeScript unit test runner component.
83 lines (82 loc) • 2.83 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var TestExecutionService = (function () {
function TestExecutionService() {
}
TestExecutionService.prototype.runTests = function (scripts) {
var _this = this;
var errors = [];
var testScripts = scripts.filter(function (script) { return script.type === 2 || script.type === 0; });
testScripts
.forEach(function (script) {
try {
_this.runTest(script);
}
catch (err) {
errors.push({
msg: err.toString(),
url: script.localPath || script.url,
line: err.lineNumber || 0
});
}
});
return errors;
};
TestExecutionService.prototype.runTest = function (script) {
if (script.localPath) {
console.log('NSUTR: require script ' + script.url + ' from ' + script.localPath);
if (!global.TNS_WEBPACK) {
require(script.localPath);
}
}
else {
if (script.shouldEval) {
console.log('NSUTR: eval script ' + script.url);
this.loadShim(script.url);
var geval = eval;
geval(script.contents);
this.completeLoading(script.url);
}
else {
console.log('NSUTR: ignoring evaluation of script ' + script.url);
}
}
};
TestExecutionService.prototype.loadShim = function (url) {
if (url.indexOf('mocha') !== -1) {
global.window = global;
global.location = { href: '/' };
global.document = {
getElementById: function (id) { return null; }
};
}
else if (url.indexOf('chai') !== -1) {
global.__shim_require = global.require;
global.require = function () {
throw Error();
};
global.window = global;
}
else if (url.indexOf('qunit.js') !== -1) {
global.define = function (factory) {
global.QUnit = factory();
};
global.define.amd = true;
}
};
TestExecutionService.prototype.completeLoading = function (url) {
if (url.indexOf('mocha') !== -1) {
delete global.window;
delete global.document;
}
if (url.indexOf('chai') !== -1) {
delete global.window;
global.require = global.__shim_require;
delete global.__shim_require;
}
else if (url.indexOf('qunit.js') !== -1) {
delete global.define;
}
};
return TestExecutionService;
}());
exports.TestExecutionService = TestExecutionService;