UNPKG

7.2 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12const cli_1 = require("./cli");
13const flagpoleexecutionoptions_1 = require("../flagpoleexecutionoptions");
14const suiteexecution_1 = require("./suiteexecution");
15class TestRunner {
16 constructor() {
17 this._suiteConfigs = {};
18 this._executionResults = [];
19 this._timeStart = Date.now();
20 this._subscribers = [];
21 }
22 get suites() {
23 let arr = [];
24 Object.keys(this._suiteConfigs).forEach(suiteName => {
25 arr.push(this._suiteConfigs[suiteName]);
26 });
27 return arr;
28 }
29 get results() {
30 return this._executionResults;
31 }
32 get exitCode() {
33 let exitCode = 0;
34 this._executionResults.forEach(result => {
35 exitCode = result.exitCode > exitCode ? result.exitCode : exitCode;
36 });
37 return exitCode;
38 }
39 get allPassing() {
40 return this._executionResults.every(result => {
41 return result.exitCode == 0;
42 });
43 }
44 subscribe(callback) {
45 this._subscribers.push(callback);
46 }
47 addSuite(suiteConfig) {
48 this._suiteConfigs[suiteConfig.name] = suiteConfig;
49 }
50 run() {
51 return __awaiter(this, void 0, void 0, function* () {
52 this._executionResults = [];
53 const totalSuites = Object.keys(this._suiteConfigs).length;
54 let count = 1;
55 for (let suiteName in this._suiteConfigs) {
56 this._publish(`Running suite ${suiteName} (${count} of ${totalSuites})...`);
57 let execution = suiteexecution_1.SuiteExecutionInline.executeSuite(this._suiteConfigs[suiteName]);
58 this._executionResults.push(yield execution.result);
59 count++;
60 }
61 this._onDone();
62 return this._executionResults;
63 });
64 }
65 runSpawn() {
66 return __awaiter(this, void 0, void 0, function* () {
67 return flagpoleexecutionoptions_1.FlagpoleExecution.opts.asyncExecution
68 ? this._runSpawnAync()
69 : this._runSpawn();
70 });
71 }
72 _runSpawn() {
73 return __awaiter(this, void 0, void 0, function* () {
74 this._executionResults = [];
75 const totalSuites = Object.keys(this._suiteConfigs).length;
76 let count = 1;
77 for (let suiteName in this._suiteConfigs) {
78 this._publish(`Running suite ${suiteName} (${count} of ${totalSuites})...`);
79 let execution = suiteexecution_1.SuiteExecution.executeSuite(this._suiteConfigs[suiteName]);
80 this._executionResults.push(yield execution.result);
81 count++;
82 }
83 this._onDone();
84 return this._executionResults;
85 });
86 }
87 _runSpawnAync() {
88 return __awaiter(this, void 0, void 0, function* () {
89 return new Promise((resolve, reject) => {
90 const totalSuites = Object.keys(this._suiteConfigs).length;
91 const suitePromises = [];
92 let count = 1;
93 for (let suiteName in this._suiteConfigs) {
94 this._publish(`Running suite ${suiteName} (${count} of ${totalSuites})...`);
95 let execution = suiteexecution_1.SuiteExecution.executeSuite(this._suiteConfigs[suiteName]);
96 suitePromises.push(execution.result);
97 count++;
98 }
99 Promise.all(suitePromises)
100 .then(results => {
101 this._executionResults = results;
102 this._onDone();
103 resolve(this._executionResults);
104 })
105 .catch(reject);
106 });
107 });
108 }
109 _onDone() {
110 const duration = Date.now() - this._timeStart;
111 let output = "";
112 if (flagpoleexecutionoptions_1.FlagpoleExecution.opts.output == flagpoleexecutionoptions_1.FlagpoleOutput.json) {
113 let suiteOutput = [];
114 let overall = {
115 pass: 0,
116 fail: 0
117 };
118 this._executionResults.forEach(result => {
119 suiteOutput.push(result.toString());
120 if (result.exitCode == 0) {
121 overall.pass += 1;
122 }
123 else {
124 overall.fail += 1;
125 }
126 });
127 output =
128 `
129 {
130 "summary": {
131 "passCount": ${overall.pass},
132 "failCount": ${overall.fail},
133 "duration": ${duration}
134 }, ` +
135 `"suites": [
136 ${suiteOutput.join(",")}
137 ]
138 }
139 `;
140 }
141 else {
142 this._executionResults.forEach(result => {
143 output += result.toString() + "\n";
144 });
145 }
146 if (flagpoleexecutionoptions_1.FlagpoleExecution.opts.output == flagpoleexecutionoptions_1.FlagpoleOutput.browser) {
147 const open = require("open");
148 const fs = require("fs");
149 const tmp = require("tmp");
150 const tmpObj = tmp.fileSync({ postfix: ".html" });
151 const filePath = tmpObj.name;
152 let template = fs.readFileSync(`${__dirname}/report.html`, "utf8");
153 template = template.replace("${output}", output).replace("${nav}", "");
154 fs.writeFileSync(filePath, template);
155 cli_1.Cli.log(`Writing output to: ${filePath}`);
156 (() => __awaiter(this, void 0, void 0, function* () {
157 yield open(filePath);
158 cli_1.Cli.exit(this.allPassing ? 0 : 1);
159 }))();
160 }
161 else {
162 cli_1.Cli.log(output);
163 if (!this.allPassing &&
164 flagpoleexecutionoptions_1.FlagpoleExecution.opts.output == flagpoleexecutionoptions_1.FlagpoleOutput.console) {
165 cli_1.Cli.log("Some suites failed.");
166 }
167 }
168 }
169 toString() {
170 let output = "";
171 this._executionResults.forEach(result => {
172 output += result.toString() + "\n";
173 });
174 return output;
175 }
176 _publish(message) {
177 this._subscribers.forEach(callback => {
178 callback.apply(this, [message]);
179 });
180 }
181}
182exports.TestRunner = TestRunner;
183//# sourceMappingURL=testrunner.js.map
\No newline at end of file