intern
Version:
Intern. A next-generation code testing stack for JavaScript.
132 lines • 6.13 kB
JavaScript
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "tslib", "minimatch", "@dojo/core/request/providers/xhr", "@dojo/core/async/Task", "@dojo/shim/global", "../common/console", "./Executor", "../common/path", "../browser/util", "../reporters/Html", "../reporters/Dom", "../reporters/Console"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var minimatch_1 = require("minimatch");
var xhr_1 = require("@dojo/core/request/providers/xhr");
var Task_1 = require("@dojo/core/async/Task");
var global_1 = require("@dojo/shim/global");
var console = require("../common/console");
var Executor_1 = require("./Executor");
var path_1 = require("../common/path");
var util_1 = require("../browser/util");
var Html_1 = require("../reporters/Html");
var Dom_1 = require("../reporters/Dom");
var Console_1 = require("../reporters/Console");
var Browser = (function (_super) {
tslib_1.__extends(Browser, _super);
function Browser(options) {
var _this = _super.call(this, {
basePath: '',
internPath: ''
}) || this;
global_1.default.addEventListener('unhandledRejection', function (event) {
console.warn('Unhandled rejection:', event);
_this.emit('error', event.reason);
});
global_1.default.addEventListener('error', function (event) {
console.warn('Unhandled error:', event);
var error = new Error(event.message);
error.stack = event.filename + ":" + event.lineno + ":" + event.colno;
_this.emit('error', error);
});
_this.registerReporter('html', function (options) { return new Html_1.default(_this, options); });
_this.registerReporter('dom', function (options) { return new Dom_1.default(_this, options); });
_this.registerReporter('console', function (options) { return new Console_1.default(_this, options); });
if (options) {
_this.configure(options);
}
return _this;
}
Object.defineProperty(Browser.prototype, "environment", {
get: function () {
return 'browser';
},
enumerable: true,
configurable: true
});
Browser.prototype.loadScript = function (script, isEsm) {
var _this = this;
if (isEsm === void 0) { isEsm = false; }
if (typeof script === 'string') {
script = [script];
}
return script.reduce(function (previous, script) {
if (script[0] !== '/') {
script = "" + _this.config.basePath + script;
}
return previous.then(function () { return injectScript(script, isEsm); });
}, Task_1.default.resolve());
};
Browser.prototype._resolveConfig = function () {
var _this = this;
return _super.prototype._resolveConfig.call(this).then(function () {
var config = _this.config;
var currentPath = global_1.default.location.pathname;
if (!config.internPath) {
if (/\.html$/.test(currentPath)) {
config.internPath = path_1.dirname(currentPath);
}
else {
config.internPath = currentPath;
}
}
if (!config.basePath) {
config.basePath = util_1.getDefaultBasePath();
}
else if (/^\./.test(config.basePath)) {
config.basePath = path_1.join(config.internPath, config.basePath);
}
['basePath', 'internPath'].forEach(function (property) {
var key = property;
config[key] = path_1.normalizePathEnding(config[key]);
});
var suites = (config.browser.suites = config.suites.concat(config.browser.suites));
delete config.suites;
var hasGlobs = suites.some(function (pattern) {
var matcher = new minimatch_1.Minimatch(pattern);
return matcher.set[0].some(function (entry) { return typeof entry !== 'string'; });
});
if (hasGlobs) {
return xhr_1.default('__resolveSuites__', { query: { suites: suites } })
.then(function (response) { return response.json(); })
.catch(function () {
throw new Error('The server does not support suite glob resolution');
})
.then(function (data) {
config.browser.suites = data;
});
}
});
};
return Browser;
}(Executor_1.default));
exports.default = Browser;
function injectScript(path, isEsm) {
return new Task_1.default(function (resolve, reject) {
var doc = global_1.default.document;
var scriptTag = doc.createElement('script');
scriptTag.addEventListener('load', function () {
resolve();
});
scriptTag.addEventListener('error', function (event) {
console.error("Error loading " + path + ":", event);
reject(new Error("Unable to load " + path));
});
if (isEsm) {
scriptTag.type = 'module';
}
scriptTag.src = path;
doc.body.appendChild(scriptTag);
});
}
});
//# sourceMappingURL=Browser.js.map