flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
190 lines • 7.29 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 });
exports.TestRunner = void 0;
const cli_1 = require("./cli");
const suiteexecution_1 = require("./suiteexecution");
const __1 = require("..");
const suiteexecutioninline_1 = require("./suiteexecutioninline");
class TestRunner {
constructor() {
this._suiteConfigs = {};
this._executionResults = [];
this._timeStart = Date.now();
this._subscribers = [];
this._finishedResolver = () => { };
this._finishedPromise = new Promise((resolve) => {
this._finishedResolver = resolve;
});
}
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;
});
}
after(callback) {
this._finishedPromise.then(callback);
}
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 = suiteexecutioninline_1.SuiteExecutionInline.executeSuite(this._suiteConfigs[suiteName]);
this._executionResults.push(yield execution.result);
count++;
}
this._onDone();
return this._executionResults;
});
}
runSpawn(asyncExecution) {
return __awaiter(this, void 0, void 0, function* () {
return 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 = "";
this._finishedResolver(this._executionResults);
if (__1.FlagpoleExecution.global.isJsonOutput) {
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 (__1.FlagpoleExecution.global.isBrowserOutput) {
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 && __1.FlagpoleExecution.global.shouldOutputToConsole) {
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