UNPKG

7.13 kBJavaScriptView Raw
1(function (factory) {
2 if (typeof module === "object" && typeof module.exports === "object") {
3 var v = factory(require, exports);
4 if (v !== undefined) module.exports = v;
5 }
6 else if (typeof define === "function" && define.amd) {
7 define(["require", "exports", "tslib", "minimatch", "@theintern/common", "../common/console", "./Executor", "../common/path", "../browser/util", "../reporters/Html", "../reporters/Dom", "../reporters/Console"], factory);
8 }
9})(function (require, exports) {
10 "use strict";
11 Object.defineProperty(exports, "__esModule", { value: true });
12 var tslib_1 = require("tslib");
13 var minimatch_1 = require("minimatch");
14 var common_1 = require("@theintern/common");
15 var console = tslib_1.__importStar(require("../common/console"));
16 var Executor_1 = tslib_1.__importDefault(require("./Executor"));
17 var path_1 = require("../common/path");
18 var util_1 = require("../browser/util");
19 var Html_1 = tslib_1.__importDefault(require("../reporters/Html"));
20 var Dom_1 = tslib_1.__importDefault(require("../reporters/Dom"));
21 var Console_1 = tslib_1.__importDefault(require("../reporters/Console"));
22 var Browser = (function (_super) {
23 tslib_1.__extends(Browser, _super);
24 function Browser(options) {
25 var _this = _super.call(this, {
26 basePath: '',
27 internPath: '',
28 }) || this;
29 common_1.global.addEventListener('unhandledRejection', function (event) {
30 console.warn('Unhandled rejection:', event);
31 var warnOnUnhandledRejection = _this.config.warnOnUnhandledRejection;
32 if (warnOnUnhandledRejection &&
33 (warnOnUnhandledRejection === true ||
34 warnOnUnhandledRejection.test("" + event.reason))) {
35 _this.emit('warning', "" + event.reason);
36 }
37 else {
38 _this.emit('error', event.reason);
39 }
40 });
41 common_1.global.addEventListener('error', function (event) {
42 console.warn('Unhandled error:', event);
43 var error = new Error(event.message);
44 if (_this.config.warnOnUncaughtException &&
45 (_this.config.warnOnUncaughtException === true ||
46 _this.config.warnOnUncaughtException.test("" + error))) {
47 _this.emit('warning', "" + error);
48 }
49 else {
50 error.stack = event.filename + ":" + event.lineno + ":" + event.colno;
51 _this.emit('error', error);
52 }
53 });
54 _this.registerReporter('html', function (options) { return new Html_1.default(_this, options); });
55 _this.registerReporter('dom', function (options) { return new Dom_1.default(_this, options); });
56 _this.registerReporter('console', function (options) { return new Console_1.default(_this, options); });
57 if (options) {
58 _this.configure(options);
59 }
60 return _this;
61 }
62 Object.defineProperty(Browser.prototype, "environment", {
63 get: function () {
64 return 'browser';
65 },
66 enumerable: false,
67 configurable: true
68 });
69 Browser.prototype.loadScript = function (script, isEsm) {
70 var _this = this;
71 if (isEsm === void 0) { isEsm = false; }
72 if (typeof script === 'string') {
73 script = [script];
74 }
75 return script.reduce(function (previous, script) {
76 if (script[0] !== '/' && !/https?:\/\//.test(script)) {
77 script = "" + _this.config.basePath + script;
78 }
79 return previous.then(function () { return injectScript(script, isEsm); });
80 }, common_1.Task.resolve());
81 };
82 Browser.prototype._resolveConfig = function () {
83 var _this = this;
84 return _super.prototype._resolveConfig.call(this).then(function () {
85 var config = _this.config;
86 if (!config.internPath) {
87 var scripts = document.scripts;
88 for (var i = 0; i < scripts.length; i++) {
89 var scriptPath = scripts[i].src;
90 if (/browser\/intern.js/.test(scriptPath)) {
91 config.internPath = path_1.dirname(path_1.dirname(scriptPath));
92 }
93 }
94 if (!config.internPath) {
95 config.internPath = '/';
96 }
97 }
98 if (!config.basePath) {
99 config.basePath = util_1.getDefaultBasePath();
100 }
101 else if (/^\./.test(config.basePath)) {
102 config.basePath = path_1.join(config.internPath, config.basePath);
103 }
104 ['basePath', 'internPath'].forEach(function (property) {
105 config[property] = path_1.normalizePathEnding(config[property]);
106 });
107 var suites = (config.browser.suites = tslib_1.__spreadArray(tslib_1.__spreadArray([], config.suites), config.browser.suites));
108 delete config.suites;
109 var hasGlobs = suites.some(function (pattern) {
110 var matcher = new minimatch_1.Minimatch(pattern);
111 return matcher.set[0].some(function (entry) { return typeof entry !== 'string'; });
112 });
113 if (hasGlobs) {
114 return common_1.request('__resolveSuites__', { query: { suites: suites } })
115 .then(function (response) { return response.json(); })
116 .catch(function () {
117 throw new Error('The server does not support suite glob resolution');
118 })
119 .then(function (data) {
120 config.browser.suites = data;
121 });
122 }
123 });
124 };
125 return Browser;
126 }(Executor_1.default));
127 exports.default = Browser;
128 function injectScript(path, isEsm) {
129 return new common_1.Task(function (resolve, reject) {
130 var doc = common_1.global.document;
131 var scriptTag = doc.createElement('script');
132 scriptTag.addEventListener('load', function () {
133 resolve();
134 });
135 scriptTag.addEventListener('error', function (event) {
136 console.error("Error loading " + path + ":", event);
137 reject(new Error("Unable to load " + path));
138 });
139 if (isEsm) {
140 scriptTag.type = 'module';
141 }
142 scriptTag.src = path;
143 scriptTag.defer = true;
144 var scriptTarget = document.head || document.body;
145 scriptTarget.appendChild(scriptTag);
146 });
147 }
148});
149//# sourceMappingURL=Browser.js.map
\No newline at end of file