UNPKG

38.6 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", "fs", "path", "resolve", "istanbul-lib-coverage", "istanbul-lib-instrument", "istanbul-lib-source-maps", "istanbul-lib-hook", "ts-node", "@theintern/common", "@theintern/leadfoot/Command", "@theintern/leadfoot/Server", "@theintern/digdug/SeleniumTunnel", "@theintern/digdug/BrowserStackTunnel", "@theintern/digdug/SauceLabsTunnel", "@theintern/digdug/TestingBotTunnel", "@theintern/digdug/CrossBrowserTestingTunnel", "@theintern/digdug/NullTunnel", "./Executor", "../common/path", "../common/util", "../node/util", "../node/ErrorFormatter", "../ProxiedSession", "../Environment", "../resolveEnvironments", "../Server", "../Suite", "../RemoteSuite", "../common/console", "../reporters/Pretty", "../reporters/Runner", "../reporters/Simple", "../reporters/JUnit", "../reporters/Cobertura", "../reporters/JsonCoverage", "../reporters/HtmlCoverage", "../reporters/Lcov", "../reporters/Benchmark", "../reporters/TeamCity"], factory);
8 }
9})(function (require, exports) {
10 "use strict";
11 Object.defineProperty(exports, "__esModule", { value: true });
12 var tslib_1 = require("tslib");
13 var fs_1 = require("fs");
14 var path_1 = require("path");
15 var resolve_1 = require("resolve");
16 var istanbul_lib_coverage_1 = require("istanbul-lib-coverage");
17 var istanbul_lib_instrument_1 = require("istanbul-lib-instrument");
18 var istanbul_lib_source_maps_1 = require("istanbul-lib-source-maps");
19 var istanbul_lib_hook_1 = require("istanbul-lib-hook");
20 var ts_node_1 = require("ts-node");
21 var common_1 = require("@theintern/common");
22 var Command_1 = tslib_1.__importDefault(require("@theintern/leadfoot/Command"));
23 var Server_1 = tslib_1.__importDefault(require("@theintern/leadfoot/Server"));
24 var SeleniumTunnel_1 = tslib_1.__importDefault(require("@theintern/digdug/SeleniumTunnel"));
25 var BrowserStackTunnel_1 = tslib_1.__importDefault(require("@theintern/digdug/BrowserStackTunnel"));
26 var SauceLabsTunnel_1 = tslib_1.__importDefault(require("@theintern/digdug/SauceLabsTunnel"));
27 var TestingBotTunnel_1 = tslib_1.__importDefault(require("@theintern/digdug/TestingBotTunnel"));
28 var CrossBrowserTestingTunnel_1 = tslib_1.__importDefault(require("@theintern/digdug/CrossBrowserTestingTunnel"));
29 var NullTunnel_1 = tslib_1.__importDefault(require("@theintern/digdug/NullTunnel"));
30 var Executor_1 = tslib_1.__importDefault(require("./Executor"));
31 var path_2 = require("../common/path");
32 var util_1 = require("../common/util");
33 var util_2 = require("../node/util");
34 var ErrorFormatter_1 = tslib_1.__importDefault(require("../node/ErrorFormatter"));
35 var ProxiedSession_1 = tslib_1.__importDefault(require("../ProxiedSession"));
36 var Environment_1 = tslib_1.__importDefault(require("../Environment"));
37 var resolveEnvironments_1 = tslib_1.__importDefault(require("../resolveEnvironments"));
38 var Server_2 = tslib_1.__importDefault(require("../Server"));
39 var Suite_1 = tslib_1.__importStar(require("../Suite"));
40 var RemoteSuite_1 = tslib_1.__importDefault(require("../RemoteSuite"));
41 var console = tslib_1.__importStar(require("../common/console"));
42 var Pretty_1 = tslib_1.__importDefault(require("../reporters/Pretty"));
43 var Runner_1 = tslib_1.__importDefault(require("../reporters/Runner"));
44 var Simple_1 = tslib_1.__importDefault(require("../reporters/Simple"));
45 var JUnit_1 = tslib_1.__importDefault(require("../reporters/JUnit"));
46 var Cobertura_1 = tslib_1.__importDefault(require("../reporters/Cobertura"));
47 var JsonCoverage_1 = tslib_1.__importDefault(require("../reporters/JsonCoverage"));
48 var HtmlCoverage_1 = tslib_1.__importDefault(require("../reporters/HtmlCoverage"));
49 var Lcov_1 = tslib_1.__importDefault(require("../reporters/Lcov"));
50 var Benchmark_1 = tslib_1.__importDefault(require("../reporters/Benchmark"));
51 var TeamCity_1 = tslib_1.__importDefault(require("../reporters/TeamCity"));
52 var process = common_1.global.process;
53 var Node = (function (_super) {
54 tslib_1.__extends(Node, _super);
55 function Node(options) {
56 var _this = _super.call(this, {
57 basePath: process.cwd() + path_1.sep,
58 capabilities: {},
59 coverage: [],
60 environments: [],
61 functionalCoverage: true,
62 functionalSuites: [],
63 functionalTimeouts: {},
64 instrumenterOptions: {},
65 maxConcurrency: Infinity,
66 name: 'node',
67 reporters: [],
68 runInSync: false,
69 serveOnly: false,
70 serverPort: 9000,
71 serverUrl: '',
72 socketPort: 9001,
73 socketTimeout: 10000,
74 tunnel: 'selenium',
75 tunnelOptions: { tunnelId: String(Date.now()) },
76 }) || this;
77 _this._coverageFiles = [];
78 _this._sourceMaps = istanbul_lib_source_maps_1.createSourceMapStore();
79 _this._instrumentedMaps = istanbul_lib_source_maps_1.createSourceMapStore();
80 _this._errorFormatter = new ErrorFormatter_1.default(_this);
81 _this._coverageMap = istanbul_lib_coverage_1.createCoverageMap();
82 _this.registerReporter('pretty', function (options) { return new Pretty_1.default(_this, options); });
83 _this.registerReporter('simple', function (options) { return new Simple_1.default(_this, options); });
84 _this.registerReporter('runner', function (options) { return new Runner_1.default(_this, options); });
85 _this.registerReporter('benchmark', function (options) { return new Benchmark_1.default(_this, options); });
86 _this.registerReporter('junit', function (options) { return new JUnit_1.default(_this, options); });
87 _this.registerReporter('jsoncoverage', function (options) { return new JsonCoverage_1.default(_this, options); });
88 _this.registerReporter('htmlcoverage', function (options) { return new HtmlCoverage_1.default(_this, options); });
89 _this.registerReporter('lcov', function (options) { return new Lcov_1.default(_this, options); });
90 _this.registerReporter('cobertura', function (options) { return new Cobertura_1.default(_this, options); });
91 _this.registerReporter('teamcity', function (options) { return new TeamCity_1.default(_this, options); });
92 _this.registerTunnel('null', NullTunnel_1.default);
93 _this.registerTunnel('selenium', SeleniumTunnel_1.default);
94 _this.registerTunnel('saucelabs', SauceLabsTunnel_1.default);
95 _this.registerTunnel('browserstack', BrowserStackTunnel_1.default);
96 _this.registerTunnel('testingbot', TestingBotTunnel_1.default);
97 _this.registerTunnel('cbt', CrossBrowserTestingTunnel_1.default);
98 if (options) {
99 _this.configure(options);
100 }
101 process.on('unhandledRejection', function (reason, promise) {
102 console.warn('Unhandled rejection:', reason, promise);
103 var warnOnUnhandledRejection = _this.config.warnOnUnhandledRejection;
104 if (warnOnUnhandledRejection &&
105 (warnOnUnhandledRejection === true ||
106 warnOnUnhandledRejection.test("" + reason))) {
107 _this.emit('warning', "" + reason);
108 }
109 else {
110 _this.emit('error', reason);
111 }
112 });
113 process.on('uncaughtException', function (reason) {
114 console.warn('Unhandled error:', reason);
115 if (_this.config.warnOnUncaughtException &&
116 (_this.config.warnOnUncaughtException === true ||
117 _this.config.warnOnUncaughtException.test("" + reason))) {
118 _this.emit('warning', "" + reason);
119 }
120 else {
121 _this.emit('error', reason);
122 }
123 });
124 _this.on('coverage', function (message) {
125 _this._coverageMap.merge(message.coverage);
126 });
127 return _this;
128 }
129 Object.defineProperty(Node.prototype, "coverageMap", {
130 get: function () {
131 return this._coverageMap;
132 },
133 enumerable: false,
134 configurable: true
135 });
136 Object.defineProperty(Node.prototype, "environment", {
137 get: function () {
138 return 'node';
139 },
140 enumerable: false,
141 configurable: true
142 });
143 Object.defineProperty(Node.prototype, "instrumentedMapStore", {
144 get: function () {
145 return this._instrumentedMaps;
146 },
147 enumerable: false,
148 configurable: true
149 });
150 Object.defineProperty(Node.prototype, "sourceMapStore", {
151 get: function () {
152 return this._sourceMaps;
153 },
154 enumerable: false,
155 configurable: true
156 });
157 Object.defineProperty(Node.prototype, "hasCoveredFiles", {
158 get: function () {
159 return this._coverageFiles.length > 0;
160 },
161 enumerable: false,
162 configurable: true
163 });
164 Object.defineProperty(Node.prototype, "suites", {
165 get: function () {
166 var suites = [];
167 if (this._rootSuite.tests.length > 0) {
168 suites.push(this._rootSuite);
169 }
170 if (this._sessionSuites) {
171 suites.push.apply(suites, this._sessionSuites);
172 }
173 return suites;
174 },
175 enumerable: false,
176 configurable: true
177 });
178 Node.prototype.addSuite = function (factory) {
179 if (this._loadingFunctionalSuites) {
180 this._sessionSuites.forEach(factory);
181 }
182 else {
183 _super.prototype.addSuite.call(this, factory);
184 }
185 };
186 Node.prototype.getTunnel = function (name) {
187 return this.getPlugin("tunnel." + name);
188 };
189 Node.prototype.instrumentCode = function (code, filename, shouldCompile) {
190 this.log('Instrumenting', filename);
191 if (filename.endsWith('.d.ts')) {
192 return code;
193 }
194 if (shouldCompile) {
195 return util_2.transpileSource(filename, code);
196 }
197 var sourceMap = util_2.readSourceMap(filename, code);
198 if (sourceMap) {
199 this._sourceMaps.registerMap(filename, sourceMap);
200 }
201 try {
202 var instrumenter = this._instrumenter;
203 var newCode = instrumenter.instrumentSync(code, path_1.normalize(filename), sourceMap);
204 this._coverageMap.addFileCoverage(instrumenter.lastFileCoverage());
205 this._instrumentedMaps.registerMap(filename, instrumenter.lastSourceMap());
206 return newCode;
207 }
208 catch (error) {
209 this.emit('warning', "Error instrumenting " + filename + ": " + error.message);
210 }
211 return code;
212 };
213 Node.prototype.loadScript = function (script) {
214 var scripts = Array.isArray(script) ? script : [script];
215 try {
216 for (var _i = 0, scripts_1 = scripts; _i < scripts_1.length; _i++) {
217 var script_1 = scripts_1[_i];
218 var file = path_1.resolve(script_1);
219 if (fs_1.existsSync(file)) {
220 require(file);
221 }
222 else {
223 require(resolve_1.sync(script_1, { basedir: process.cwd() }));
224 }
225 }
226 }
227 catch (error) {
228 return common_1.Task.reject(error);
229 }
230 return common_1.Task.resolve();
231 };
232 Node.prototype.registerTunnel = function (name, Ctor) {
233 this.registerPlugin('tunnel', name, function () { return Ctor; });
234 };
235 Node.prototype.shouldInstrumentFile = function (filename) {
236 if (this._coverageFiles.indexOf(filename) !== -1) {
237 return !(filename in this._instrumentedMaps.data);
238 }
239 return false;
240 };
241 Node.prototype._afterRun = function () {
242 var _this = this;
243 return _super.prototype._afterRun.call(this).finally(function () {
244 _this._removeInstrumentationHooks();
245 var promises = [];
246 if (_this.server) {
247 promises.push(_this.server
248 .stop()
249 .then(function () { return _this.emit('serverEnd', _this.server); }));
250 }
251 if (_this.tunnel) {
252 var tunnel_1 = _this.tunnel;
253 promises.push(tunnel_1.stop().then(function () { return _this.emit('tunnelStop', { tunnel: tunnel_1 }); }));
254 }
255 return Promise.all(promises).then(function () { }, function (error) { return _this.emit('error', error); });
256 });
257 };
258 Node.prototype._beforeRun = function () {
259 var _this = this;
260 return _super.prototype._beforeRun.call(this).then(function () {
261 var config = _this.config;
262 var suite = _this._rootSuite;
263 suite.grep = config.grep;
264 suite.timeout = config.defaultTimeout;
265 suite.bail = config.bail;
266 if ((config.environments.filter(isRemoteEnvironment).length > 0 &&
267 config.functionalSuites.length + config.browser.suites.length > 0) ||
268 config.serveOnly) {
269 var serverTask = new common_1.Task(function (resolve, reject) {
270 var server = new Server_2.default({
271 basePath: config.basePath,
272 executor: _this,
273 port: config.serverPort,
274 runInSync: config.runInSync,
275 socketPort: config.socketPort,
276 });
277 server
278 .start()
279 .then(function () {
280 _this.server = server;
281 return _this.emit('serverStart', server);
282 })
283 .then(resolve, reject);
284 });
285 if (config.serveOnly) {
286 return serverTask.then(function () {
287 return new common_1.Task(function (resolve) {
288 process.on('SIGINT', function () {
289 resolve(true);
290 });
291 });
292 });
293 }
294 return serverTask
295 .then(function () {
296 var tunnel = _this.tunnel;
297 _this._createSessionSuites();
298 return tunnel
299 .start()
300 .then(function () { return _this.emit('tunnelStart', { tunnel: tunnel }); });
301 })
302 .then(function () {
303 return false;
304 });
305 }
306 return false;
307 });
308 };
309 Node.prototype._createTunnel = function () {
310 var _this = this;
311 var config = this.config;
312 var tunnelOptions = config.tunnelOptions;
313 if (config.tunnel === 'browserstack') {
314 var options = tunnelOptions;
315 options.servers = options.servers || [];
316 options.servers.push(config.serverUrl);
317 }
318 if ('proxy' in config && !('proxy' in tunnelOptions)) {
319 tunnelOptions.proxy = config.proxy;
320 }
321 var TunnelConstructor = this.getTunnel(config.tunnel);
322 var tunnel = new TunnelConstructor(this.config.tunnelOptions);
323 this.tunnel = tunnel;
324 tunnel.on('downloadprogress', function (progress) {
325 _this.emit('tunnelDownloadProgress', {
326 tunnel: tunnel,
327 progress: progress,
328 });
329 });
330 tunnel.on('status', function (status) {
331 _this.emit('tunnelStatus', {
332 tunnel: tunnel,
333 status: status.status,
334 });
335 });
336 return tunnel;
337 };
338 Node.prototype._createSessionSuites = function () {
339 var _this = this;
340 if (!this.tunnel) {
341 this.log('No tunnel - Not creating session suites');
342 return;
343 }
344 var tunnel = this.tunnel;
345 var config = this.config;
346 var leadfootServer = new Server_1.default(tunnel.clientUrl, {
347 proxy: 'proxy' in config ? config.proxy : tunnel.proxy,
348 });
349 var executor = this;
350 var InitializedProxiedSession = (function (_super) {
351 tslib_1.__extends(InitializedProxiedSession, _super);
352 function InitializedProxiedSession() {
353 var _this = _super !== null && _super.apply(this, arguments) || this;
354 _this.executor = executor;
355 _this.coverageVariable = config.coverageVariable;
356 _this.baseUrl = config.functionalBaseUrl || config.serverUrl;
357 return _this;
358 }
359 return InitializedProxiedSession;
360 }(ProxiedSession_1.default));
361 leadfootServer.sessionConstructor = InitializedProxiedSession;
362 this._sessionSuites = this.config.environments
363 .filter(isRemoteEnvironment)
364 .map(function (environmentType) {
365 var session;
366 var suite = new Suite_1.default({
367 name: String(environmentType),
368 publishAfterSetup: true,
369 grep: config.grep,
370 bail: config.bail,
371 tests: [],
372 timeout: config.defaultTimeout,
373 executor: _this,
374 before: function () {
375 var _this = this;
376 executor.log('Creating session for', environmentType);
377 return leadfootServer
378 .createSession(environmentType)
379 .then(function (_session) {
380 session = _session;
381 _this.executor.log('Created session:', session.capabilities);
382 var remote = new Command_1.default(session);
383 remote.environmentType = new Environment_1.default(session.capabilities);
384 remote.requestedEnvironment = environmentType;
385 _this.remote = remote;
386 _this.sessionId = remote.session.sessionId;
387 _this.name = remote.environmentType.toString();
388 var timeouts = config.functionalTimeouts;
389 var promise = Promise.resolve();
390 if (timeouts.executeAsync != null) {
391 promise = promise.then(function () {
392 return remote.setExecuteAsyncTimeout(timeouts.executeAsync);
393 });
394 _this.executor.log('Set remote executeAsync timeout to ', timeouts.executeAsync);
395 }
396 if (timeouts.find != null) {
397 promise = promise.then(function () {
398 return remote.setFindTimeout(timeouts.find);
399 });
400 _this.executor.log('Set remote find timeout to ', timeouts.find);
401 }
402 if (timeouts.pageLoad != null) {
403 promise = promise.then(function () {
404 return remote.setPageLoadTimeout(timeouts.pageLoad);
405 });
406 _this.executor.log('Set remote pageLoad timeout to ', timeouts.pageLoad);
407 }
408 return promise;
409 });
410 },
411 after: function () {
412 var _this = this;
413 var remote = this.remote;
414 if (remote != null) {
415 var endSession = function () {
416 function hasError(suite) {
417 if (suite.error != null || suite.numFailedTests > 0) {
418 return true;
419 }
420 return suite.tests.filter(Suite_1.isSuite).some(hasError);
421 }
422 return tunnel.sendJobState(remote.session.sessionId, {
423 success: !hasError(_this),
424 });
425 };
426 if (config.leaveRemoteOpen === true ||
427 (config.leaveRemoteOpen === 'fail' && this.numFailedTests > 0)) {
428 return endSession();
429 }
430 return remote.quit().finally(endSession);
431 }
432 },
433 });
434 if (config.browser.suites.length > 0) {
435 suite.add(new RemoteSuite_1.default());
436 }
437 return suite;
438 });
439 };
440 Node.prototype._loadFunctionalSuites = function () {
441 var _this = this;
442 this._loadingFunctionalSuites = true;
443 var suites = this.config.functionalSuites;
444 if (this.config.functionalCoverage !== false &&
445 !this._unhookRequire &&
446 this.hasCoveredFiles) {
447 this._setInstrumentationHooks();
448 }
449 return common_1.Task.resolve(this._loader(suites))
450 .then(function () {
451 _this.log('Loaded functional suites:', suites);
452 })
453 .finally(function () {
454 _this._loadingFunctionalSuites = false;
455 });
456 };
457 Node.prototype._loadSuites = function () {
458 if (!this.config.environments.some(isLocalEnvironment) ||
459 this.config.serveOnly) {
460 return common_1.Task.resolve();
461 }
462 if (this.hasCoveredFiles) {
463 this._setInstrumentationHooks();
464 }
465 return _super.prototype._loadSuites.call(this);
466 };
467 Node.prototype._resolveConfig = function () {
468 var _this = this;
469 return _super.prototype._resolveConfig.call(this).then(function () {
470 var config = _this.config;
471 if (config.environments.length === 0) {
472 _this.log("Adding default 'node' environment");
473 config.environments.push({ browserName: 'node' });
474 }
475 config.environments.forEach(function (env) {
476 var browserName = env.browserName;
477 var newName = getNormalizedBrowserName(browserName);
478 env.browserName = newName;
479 if (env.browser) {
480 env.browser = newName;
481 }
482 });
483 if (config.tunnelOptions.drivers) {
484 config.tunnelOptions.drivers = config.tunnelOptions.drivers.map(function (driver) {
485 var driverName;
486 if (typeof driver === 'string') {
487 driverName = driver;
488 }
489 else if ('name' in driver) {
490 driverName = driver.name;
491 }
492 var newName = getNormalizedBrowserName(driverName);
493 if (typeof driver === 'string') {
494 return newName;
495 }
496 if ('name' in driver) {
497 return tslib_1.__assign(tslib_1.__assign({}, driver), { name: newName });
498 }
499 return driver;
500 });
501 }
502 if (!config.internPath) {
503 config.internPath = path_1.dirname(path_1.dirname(__dirname));
504 if (config.internPath.indexOf(process.cwd()) !== 0) {
505 config.internPath = path_1.dirname(resolve_1.sync('intern', {
506 basedir: process.cwd(),
507 }));
508 }
509 }
510 ['basePath', 'internPath'].forEach(function (property) {
511 config[property] = path_2.normalizePathEnding(path_1.resolve(config[property]), path_1.sep);
512 });
513 if (config.benchmarkConfig) {
514 config.reporters.push({
515 name: 'benchmark',
516 options: config.benchmarkConfig,
517 });
518 }
519 _this._instrumentBasePath = config.basePath;
520 if (config.coverage) {
521 _this._coverageFiles = util_2.expandFiles(config.coverage).map(function (path) {
522 return path_1.resolve(path);
523 });
524 }
525 if (!config.serverUrl) {
526 config.serverUrl = "http://localhost:" + config.serverPort + "/";
527 }
528 if (config.connectTimeout == null) {
529 config.connectTimeout = 30000;
530 }
531 if (config.heartbeatInterval == null) {
532 var idleTimeout = config.capabilities['idle-timeout'];
533 config.heartbeatInterval = idleTimeout == null ? 60 : idleTimeout;
534 }
535 ['serverUrl', 'functionalBaseUrl'].forEach(function (property) {
536 if (config[property]) {
537 config[property] = config[property].replace(/\/*$/, '/');
538 }
539 });
540 if (!config.capabilities.name) {
541 config.capabilities.name = 'intern';
542 }
543 if (config.capabilities['idle-timeout'] == null) {
544 config.capabilities['idle-timeout'] = config.heartbeatInterval;
545 }
546 var buildId = process.env.TRAVIS_COMMIT || process.env.BUILD_TAG;
547 if (buildId) {
548 config.capabilities.build = buildId;
549 }
550 config.functionalSuites = util_2.expandFiles(config.functionalSuites);
551 config.node.suites = util_2.expandFiles(tslib_1.__spreadArray(tslib_1.__spreadArray([], config.suites), config.node.suites));
552 config.browser.suites = util_2.expandFiles(tslib_1.__spreadArray(tslib_1.__spreadArray([], config.suites), config.browser.suites));
553 delete config.suites;
554 if (!require.extensions['.ts']) {
555 if (((config.node &&
556 config.node.suites.some(function (pattern) { return pattern.endsWith('.ts'); })) ||
557 (config.plugins &&
558 config.plugins.some(function (plugin) {
559 return plugin.script.endsWith('.ts');
560 }))) &&
561 typeof _this.config.node.tsconfig === 'undefined') {
562 ts_node_1.register();
563 }
564 else if (_this.config.node.tsconfig) {
565 ts_node_1.register({ project: _this.config.node.tsconfig });
566 }
567 }
568 _this._instrumenter = istanbul_lib_instrument_1.createInstrumenter(Object.assign(tslib_1.__assign({ esModules: true, coverageVariable: config.coverageVariable }, config.instrumenterOptions), {
569 preserveComments: true,
570 produceSourceMap: true,
571 }));
572 if (config.tunnel === 'selenium') {
573 var driverNames_1 = _this._getSeleniumDriverNames();
574 var tunnelOptions = config.tunnelOptions;
575 if (tunnelOptions.drivers) {
576 tunnelOptions.drivers
577 .map(function (driver) {
578 if (typeof driver === 'string') {
579 return driver;
580 }
581 return driver.name;
582 })
583 .filter(function (name) { return name; })
584 .forEach(function (name) {
585 var index = driverNames_1.indexOf(name);
586 if (index !== -1) {
587 driverNames_1.splice(index, 1);
588 }
589 });
590 tunnelOptions.drivers = tslib_1.__spreadArray(tslib_1.__spreadArray([], tunnelOptions.drivers), driverNames_1.map(function (name) { return ({ name: name }); }));
591 }
592 else {
593 tunnelOptions.drivers = driverNames_1.map(function (name) { return ({ name: name }); });
594 }
595 }
596 var remoteEnvironments = config.environments.filter(isRemoteEnvironment);
597 if (remoteEnvironments.length > 0 && config.tunnel && !config.serveOnly) {
598 var tunnel = _this._createTunnel();
599 config.capabilities = common_1.deepMixin(tunnel.extraCapabilities, config.capabilities);
600 return tunnel.getEnvironments().then(function (tunnelEnvironments) {
601 var resolvedEnvironments = resolveEnvironments_1.default(config.capabilities, remoteEnvironments, tunnelEnvironments);
602 var localEnvironments = config.environments.filter(function (env) { return !isRemoteEnvironment(env); });
603 config.environments = tslib_1.__spreadArray(tslib_1.__spreadArray([], localEnvironments), resolvedEnvironments);
604 });
605 }
606 });
607 };
608 Node.prototype._getSeleniumDriverNames = function () {
609 var config = this.config;
610 var driverNames = new Set();
611 for (var _i = 0, _a = config.environments; _i < _a.length; _i++) {
612 var env = _a[_i];
613 var browserName = env.browserName;
614 if (browserName === 'chrome' ||
615 browserName === 'firefox' ||
616 browserName === 'internet explorer') {
617 driverNames.add(browserName);
618 }
619 else if (browserName === 'MicrosoftEdge') {
620 var browserVersion = env.browserVersion;
621 if ((!isNaN(browserVersion) && Number(browserVersion) < 1000) ||
622 (isNaN(browserVersion) && browserVersion === 'insider preview')) {
623 driverNames.add('MicrosoftEdgeChromium');
624 }
625 else {
626 driverNames.add('MicrosoftEdge');
627 }
628 }
629 }
630 return Array.from(driverNames);
631 };
632 Node.prototype._runTests = function () {
633 var _this = this;
634 var testTask;
635 return new common_1.Task(function (resolve, reject) {
636 if (_this._rootSuite.tests.length > 0) {
637 testTask = _this._rootSuite.run();
638 }
639 else {
640 testTask = common_1.Task.resolve();
641 }
642 testTask
643 .then(function () {
644 if (!_this._sessionSuites) {
645 return;
646 }
647 return _this._loadFunctionalSuites().then(function () { return (testTask = _this._runRemoteTests()); });
648 })
649 .then(resolve, reject);
650 }, function () {
651 if (testTask) {
652 testTask.cancel();
653 }
654 }).finally(function () {
655 var coveredFiles = _this._coverageMap.files();
656 var uncoveredFiles = _this._coverageFiles.filter(function (filename) {
657 return coveredFiles.indexOf(filename) === -1;
658 });
659 uncoveredFiles.forEach(function (filename) {
660 try {
661 var code = fs_1.readFileSync(filename, { encoding: 'utf8' });
662 _this.instrumentCode(code, filename, filename.endsWith('.ts') || filename.endsWith('.tsx'));
663 }
664 catch (_error) { }
665 });
666 });
667 };
668 Node.prototype._runRemoteTests = function () {
669 var _this = this;
670 var config = this.config;
671 var sessionSuites = this._sessionSuites;
672 var queue = new FunctionQueue(config.maxConcurrency || Infinity);
673 this.log('Running', sessionSuites.length, 'suites with maxConcurrency', config.maxConcurrency);
674 var runTask = new common_1.Task(function (resolve, reject) {
675 common_1.Task.all(sessionSuites.map(function (suite) {
676 _this.log('Queueing suite', suite.name);
677 return queue.enqueue(function () {
678 _this.log('Running suite', suite.name);
679 return suite.run();
680 });
681 })).then(resolve, reject);
682 }, function () {
683 _this.log('Canceling remote tests');
684 queue.clear();
685 });
686 return runTask
687 .then(function () { })
688 .finally(function () {
689 if (config.functionalCoverage !== false) {
690 _this.log('Emitting functional coverage');
691 return _this._emitCoverage('functional tests');
692 }
693 });
694 };
695 Node.prototype._setInstrumentationHooks = function () {
696 var _this = this;
697 istanbul_lib_hook_1.hookRunInThisContext(function (filename) { return _this.shouldInstrumentFile(filename); }, function (code, _a) {
698 var filename = _a.filename;
699 return _this.instrumentCode(code, filename);
700 });
701 this._unhookRequire = istanbul_lib_hook_1.hookRequire(function (filename) { return _this.shouldInstrumentFile(filename); }, function (code, _a) {
702 var filename = _a.filename;
703 return _this.instrumentCode(code, filename);
704 }, { extensions: ['.js', '.jsx', '.ts', 'tsx'] });
705 };
706 Node.prototype._removeInstrumentationHooks = function () {
707 istanbul_lib_hook_1.unhookRunInThisContext();
708 if (this._unhookRequire) {
709 this._unhookRequire();
710 this._unhookRequire = undefined;
711 }
712 };
713 return Node;
714 }(Executor_1.default));
715 exports.default = Node;
716 var FunctionQueue = (function () {
717 function FunctionQueue(maxConcurrency) {
718 this.maxConcurrency = maxConcurrency;
719 this.queue = [];
720 this.activeTasks = [];
721 this.funcTasks = [];
722 }
723 FunctionQueue.prototype.enqueue = function (func) {
724 var _this = this;
725 var funcTask = new common_1.Task(function (resolve, reject) {
726 _this.queue.push({ func: func, resolve: resolve, reject: reject });
727 });
728 this.funcTasks.push(funcTask);
729 if (this.activeTasks.length < this.maxConcurrency) {
730 this.next();
731 }
732 return funcTask;
733 };
734 FunctionQueue.prototype.clear = function () {
735 this.activeTasks.forEach(function (task) { return task.cancel(); });
736 this.funcTasks.forEach(function (task) { return task.cancel(); });
737 this.activeTasks = [];
738 this.funcTasks = [];
739 this.queue = [];
740 };
741 FunctionQueue.prototype.next = function () {
742 var _this = this;
743 if (this.queue.length > 0) {
744 var _a = this.queue.shift(), func = _a.func, resolve_2 = _a.resolve, reject = _a.reject;
745 var task_1 = func()
746 .then(resolve_2, reject)
747 .finally(function () {
748 util_1.pullFromArray(_this.activeTasks, task_1);
749 _this.next();
750 });
751 this.activeTasks.push(task_1);
752 }
753 };
754 return FunctionQueue;
755 }());
756 function isRemoteEnvironment(environment) {
757 return environment.browserName !== 'node';
758 }
759 function isLocalEnvironment(environment) {
760 return !isRemoteEnvironment(environment);
761 }
762 function getNormalizedBrowserName(name) {
763 if (name === 'ie') {
764 return 'internet explorer';
765 }
766 if (name && /^edge/.test(name)) {
767 return name.replace(/^edge/, 'MicrosoftEdge');
768 }
769 return name;
770 }
771});
772//# sourceMappingURL=Node.js.map
\No newline at end of file