UNPKG

intern

Version:

Intern. A next-generation code testing stack for JavaScript.

149 lines 7.13 kB
(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", "@theintern/common", "../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 common_1 = require("@theintern/common"); var console = tslib_1.__importStar(require("../common/console")); var Executor_1 = tslib_1.__importDefault(require("./Executor")); var path_1 = require("../common/path"); var util_1 = require("../browser/util"); var Html_1 = tslib_1.__importDefault(require("../reporters/Html")); var Dom_1 = tslib_1.__importDefault(require("../reporters/Dom")); var Console_1 = tslib_1.__importDefault(require("../reporters/Console")); var Browser = (function (_super) { tslib_1.__extends(Browser, _super); function Browser(options) { var _this = _super.call(this, { basePath: '', internPath: '', }) || this; common_1.global.addEventListener('unhandledRejection', function (event) { console.warn('Unhandled rejection:', event); var warnOnUnhandledRejection = _this.config.warnOnUnhandledRejection; if (warnOnUnhandledRejection && (warnOnUnhandledRejection === true || warnOnUnhandledRejection.test("" + event.reason))) { _this.emit('warning', "" + event.reason); } else { _this.emit('error', event.reason); } }); common_1.global.addEventListener('error', function (event) { console.warn('Unhandled error:', event); var error = new Error(event.message); if (_this.config.warnOnUncaughtException && (_this.config.warnOnUncaughtException === true || _this.config.warnOnUncaughtException.test("" + error))) { _this.emit('warning', "" + error); } else { 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: false, 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] !== '/' && !/https?:\/\//.test(script)) { script = "" + _this.config.basePath + script; } return previous.then(function () { return injectScript(script, isEsm); }); }, common_1.Task.resolve()); }; Browser.prototype._resolveConfig = function () { var _this = this; return _super.prototype._resolveConfig.call(this).then(function () { var config = _this.config; if (!config.internPath) { var scripts = document.scripts; for (var i = 0; i < scripts.length; i++) { var scriptPath = scripts[i].src; if (/browser\/intern.js/.test(scriptPath)) { config.internPath = path_1.dirname(path_1.dirname(scriptPath)); } } if (!config.internPath) { config.internPath = '/'; } } 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) { config[property] = path_1.normalizePathEnding(config[property]); }); var suites = (config.browser.suites = tslib_1.__spreadArray(tslib_1.__spreadArray([], config.suites), 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 common_1.request('__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 common_1.Task(function (resolve, reject) { var doc = common_1.global.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; scriptTag.defer = true; var scriptTarget = document.head || document.body; scriptTarget.appendChild(scriptTag); }); } }); //# sourceMappingURL=Browser.js.map