flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
183 lines • 7.2 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const cli_1 = require("./cli");
const flagpoleexecutionoptions_1 = require("../flagpoleexecutionoptions");
const suiteexecution_1 = require("./suiteexecution");
class TestRunner {
constructor() {
this._suiteConfigs = {};
this._executionResults = [];
this._timeStart = Date.now();
this._subscribers = [];
}
get suites() {
let arr = [];
Object.keys(this._suiteConfigs).forEach(suiteName => {
arr.push(this._suiteConfigs[suiteName]);
});
return arr;
}
get results() {
return this._executionResults;
}
get exitCode() {
let exitCode = 0;
this._executionResults.forEach(result => {
exitCode = result.exitCode > exitCode ? result.exitCode : exitCode;
});
return exitCode;
}
get allPassing() {
return this._executionResults.every(result => {
return result.exitCode == 0;
});
}
subscribe(callback) {
this._subscribers.push(callback);
}
addSuite(suiteConfig) {
this._suiteConfigs[suiteConfig.name] = suiteConfig;
}
run() {
return __awaiter(this, void 0, void 0, function* () {
this._executionResults = [];
const totalSuites = Object.keys(this._suiteConfigs).length;
let count = 1;
for (let suiteName in this._suiteConfigs) {
this._publish(`Running suite ${suiteName} (${count} of ${totalSuites})...`);
let execution = suiteexecution_1.SuiteExecutionInline.executeSuite(this._suiteConfigs[suiteName]);
this._executionResults.push(yield execution.result);
count++;
}
this._onDone();
return this._executionResults;
});
}
runSpawn() {
return __awaiter(this, void 0, void 0, function* () {
return flagpoleexecutionoptions_1.FlagpoleExecution.opts.asyncExecution
? this._runSpawnAync()
: this._runSpawn();
});
}
_runSpawn() {
return __awaiter(this, void 0, void 0, function* () {
this._executionResults = [];
const totalSuites = Object.keys(this._suiteConfigs).length;
let count = 1;
for (let suiteName in this._suiteConfigs) {
this._publish(`Running suite ${suiteName} (${count} of ${totalSuites})...`);
let execution = suiteexecution_1.SuiteExecution.executeSuite(this._suiteConfigs[suiteName]);
this._executionResults.push(yield execution.result);
count++;
}
this._onDone();
return this._executionResults;
});
}
_runSpawnAync() {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
const totalSuites = Object.keys(this._suiteConfigs).length;
const suitePromises = [];
let count = 1;
for (let suiteName in this._suiteConfigs) {
this._publish(`Running suite ${suiteName} (${count} of ${totalSuites})...`);
let execution = suiteexecution_1.SuiteExecution.executeSuite(this._suiteConfigs[suiteName]);
suitePromises.push(execution.result);
count++;
}
Promise.all(suitePromises)
.then(results => {
this._executionResults = results;
this._onDone();
resolve(this._executionResults);
})
.catch(reject);
});
});
}
_onDone() {
const duration = Date.now() - this._timeStart;
let output = "";
if (flagpoleexecutionoptions_1.FlagpoleExecution.opts.output == flagpoleexecutionoptions_1.FlagpoleOutput.json) {
let suiteOutput = [];
let overall = {
pass: 0,
fail: 0
};
this._executionResults.forEach(result => {
suiteOutput.push(result.toString());
if (result.exitCode == 0) {
overall.pass += 1;
}
else {
overall.fail += 1;
}
});
output =
`
{
"summary": {
"passCount": ${overall.pass},
"failCount": ${overall.fail},
"duration": ${duration}
}, ` +
`"suites": [
${suiteOutput.join(",")}
]
}
`;
}
else {
this._executionResults.forEach(result => {
output += result.toString() + "\n";
});
}
if (flagpoleexecutionoptions_1.FlagpoleExecution.opts.output == flagpoleexecutionoptions_1.FlagpoleOutput.browser) {
const open = require("open");
const fs = require("fs");
const tmp = require("tmp");
const tmpObj = tmp.fileSync({ postfix: ".html" });
const filePath = tmpObj.name;
let template = fs.readFileSync(`${__dirname}/report.html`, "utf8");
template = template.replace("${output}", output).replace("${nav}", "");
fs.writeFileSync(filePath, template);
cli_1.Cli.log(`Writing output to: ${filePath}`);
(() => __awaiter(this, void 0, void 0, function* () {
yield open(filePath);
cli_1.Cli.exit(this.allPassing ? 0 : 1);
}))();
}
else {
cli_1.Cli.log(output);
if (!this.allPassing &&
flagpoleexecutionoptions_1.FlagpoleExecution.opts.output == flagpoleexecutionoptions_1.FlagpoleOutput.console) {
cli_1.Cli.log("Some suites failed.");
}
}
}
toString() {
let output = "";
this._executionResults.forEach(result => {
output += result.toString() + "\n";
});
return output;
}
_publish(message) {
this._subscribers.forEach(callback => {
callback.apply(this, [message]);
});
}
}
exports.TestRunner = TestRunner;
//# sourceMappingURL=testrunner.js.map