flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
234 lines • 9.34 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 suite_execution_1 = require("./suite-execution");
const __1 = require("..");
const suite_execution_inline_1 = require("./suite-execution-inline");
const cli_ansi_1 = require("cli-ansi");
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() {
const 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 (const suiteName in this._suiteConfigs) {
this._publish(`Running suite ${suiteName} (${count} of ${totalSuites})...`);
const execution = suite_execution_inline_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 (const suiteName in this._suiteConfigs) {
this._publish(`Running suite ${suiteName} (${count} of ${totalSuites})...`);
const execution = suite_execution_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 (const suiteName in this._suiteConfigs) {
this._publish(`Running suite ${suiteName} (${count} of ${totalSuites})...`);
const execution = suite_execution_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);
});
});
}
_getSummary() {
const duration = Date.now() - this._timeStart;
let pass = 0;
let fail = 0;
this._executionResults.forEach((result) => {
if (result.exitCode == 0) {
pass += 1;
}
else {
fail += 1;
}
});
return { duration, pass, fail };
}
_onDone() {
let output = "";
this._finishedResolver(this._executionResults);
if (__1.FlagpoleExecution.global.isJsonOutput) {
const suiteOutput = [];
this._executionResults.forEach((result) => {
suiteOutput.push(result.toString());
});
const overall = this._getSummary();
output =
`
{
"summary": {
"passCount": ${overall.pass},
"failCount": ${overall.fail},
"duration": ${overall.duration}
}, ` +
`"suites": [
${suiteOutput.join(",")}
]
}
`;
}
else if (__1.FlagpoleExecution.global.isCiOutput) {
this._executionResults.forEach((result) => {
const stringifiedResult = result.toString();
if (stringifiedResult) {
output += stringifiedResult + "\n\n";
}
});
}
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}/web/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 if (__1.FlagpoleExecution.global.isXmlOutput) {
const path = require("path");
const { ensureDirSync, readFileSync, writeFileSync, } = require("fs-extra");
const reportsFolder = __1.FlagpoleExecution.global.config.getReportsFolder();
ensureDirSync(reportsFolder);
const reportFileName = `${this._timeStart}-report.xml`;
const filePath = path.join(reportsFolder, reportFileName);
let template = readFileSync(`${__dirname}/web/report.xml`, "utf8");
template = template.replace("${output}", output);
writeFileSync(filePath, template);
cli_1.Cli.log(template);
if (this.allPassing) {
cli_1.Cli.log("All suites passed.");
}
else {
cli_1.Cli.log("Some suites failed.");
}
cli_1.Cli.log(`Writing output to: ${filePath}.`);
cli_1.Cli.exit(this.allPassing ? 0 : 1);
}
else if (__1.FlagpoleExecution.global.isCiOutput) {
const overall = this._getSummary();
cli_ansi_1.default.writeLine(cli_ansi_1.default.cursorUp(), cli_ansi_1.default.eraseLine());
cli_1.Cli.subheader("SUMMARY");
cli_1.Cli.log(`Passed: ${overall.pass}`);
cli_1.Cli.log(`Failed: ${overall.fail}`);
cli_1.Cli.log(`Duration: ${overall.duration}ms`);
cli_1.Cli.log("\n");
cli_1.Cli.log(output);
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=test-runner.js.map