flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
203 lines • 8.6 kB
JavaScript
;
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 command_1 = require("../command");
const cli_1 = require("../cli");
const build_1 = require("./build");
const prompts = require("prompts");
const cli_helper_1 = require("../cli-helper");
const flagpole_execution_1 = require("../../flagpole-execution");
const cli_ansi_1 = require("cli-ansi");
const test_runner_1 = require("../test-runner");
const verbosity_1 = require("../../logging/verbosity");
class Run extends command_1.Command {
constructor() {
super(...arguments);
this.commandString = "run";
this.description = "run test suites";
this.options = [
new command_1.CliCommandOption({
flags: "--all",
description: "run all tests",
default: false,
}),
new command_1.CliCommandOption({
flags: "-a, --async",
description: "run test suites asynchronously",
default: false,
}),
new command_1.CliCommandOption({
flags: "-o, --output <format>",
description: "set output format",
default: "console",
}),
new command_1.CliCommandOption({
flags: "-s, --suite <suite>",
description: "run these test suites, supports wildcard",
}),
new command_1.CliCommandOption({
flags: "-t, --tag <tag>",
description: "run test suites with this tag",
}),
new command_1.CliCommandOption({
flags: "--build",
description: "build first",
}),
new command_1.CliCommandOption({
flags: "--headed",
description: "override tests to show a headful browser",
}),
new command_1.CliCommandOption({
flags: "--headless",
description: "override tests to keep browser headless",
}),
new command_1.CliCommandOption({
flags: "--keepCache",
description: "do not clear previous cache",
}),
];
}
helpCallback() {
console.log("");
console.log("Examples:");
console.log("");
console.log("flagpole run --build --all");
console.log("flagpole run -t smoke");
console.log("flagpole run -s browser/*");
console.log("");
}
action(args) {
return __awaiter(this, void 0, void 0, function* () {
if (args.headed || args.headless) {
flagpole_execution_1.FlagpoleExecution.global.headless = !!args.headless || !args.headed;
}
if (args.output) {
flagpole_execution_1.FlagpoleExecution.global.outputFormat = args.output;
}
if (args.build) {
yield build_1.tsc(false);
}
cli_1.Cli.subheader("Run Test Suites");
const suitesInProject = flagpole_execution_1.FlagpoleExecution.global.config.getSuites() || [];
let selectedSuites = suitesInProject;
let tag = args.tag || "";
let suiteNames = args.suite ? args.suite.split(",") : [];
if (!args.all) {
if (!suiteNames.length && !tag.length) {
const whatToRun = yield promptForWhatToRun();
if (whatToRun === "By Tag") {
tag = yield promptForTag();
}
else if (whatToRun === "Choose Suites") {
suiteNames = yield promptForSuites(selectedSuites);
if (suiteNames.length == 0) {
selectedSuites = [];
}
}
else if (whatToRun === "None") {
selectedSuites = [];
}
}
selectedSuites = filterBySuiteName(selectedSuites, suiteNames);
selectedSuites = filterByTag(selectedSuites, tag);
}
if (selectedSuites.length) {
return runSuites(selectedSuites, !!args.async, !!args.keepCache);
}
cli_1.Cli.log("No tests selected to run.").exit(0);
});
}
}
exports.default = Run;
const filterBySuiteName = (selectedSuites, suiteNames) => {
if (suiteNames && suiteNames.length > 0) {
const regExes = (() => {
const regExes = [];
(suiteNames || []).forEach((name) => {
const regexString = name.replace(".", ".").replace("*", ".*");
regExes.push(new RegExp(`^${regexString}$`, "i"));
});
return regExes;
})();
selectedSuites = selectedSuites.filter((suite) => {
return regExes.some((regEx) => {
return regEx.test(suite.name);
});
});
}
return selectedSuites;
};
const filterByTag = (selectedSuites, tag) => {
if (tag && tag.length) {
selectedSuites = selectedSuites.filter((suite) => {
return suite.tags.includes(tag);
});
}
return selectedSuites;
};
const promptForWhatToRun = () => __awaiter(void 0, void 0, void 0, function* () {
const response = yield prompts(cli_helper_1.promptSelect("run", "What tests do you want to run?", cli_helper_1.stringArrayToPromptChoices(["All", "Choose Suites", "By Tag", "None"]), 0));
return response.run;
});
const promptForSuites = (selectedSuites) => __awaiter(void 0, void 0, void 0, function* () {
const response = yield prompts({
type: "multiselect",
name: "suitesNames",
message: "Which suites do you want to run?",
choices: cli_helper_1.stringArrayToPromptChoices(selectedSuites.map((suite) => {
return suite.name;
})),
});
return response.suitesNames || [];
});
const promptForTag = () => __awaiter(void 0, void 0, void 0, function* () {
const tags = flagpole_execution_1.FlagpoleExecution.global.config.getTags() || [];
if (tags.length > 0) {
const response = yield prompts(cli_helper_1.promptSelect("tag", "What tag do you want to run?", cli_helper_1.stringArrayToPromptChoices(tags)));
return response.tag;
}
return "";
});
const runSuites = (selectedSuites, asyncExecution, keepCache) => __awaiter(void 0, void 0, void 0, function* () {
const runner = new test_runner_1.TestRunner();
selectedSuites.forEach(function (suite) {
runner.addSuite(suite);
});
if (flagpole_execution_1.FlagpoleExecution.global.shouldOutputToConsole) {
if (runner.suites.length == 0) {
cli_1.Cli.log("Did not find any test suites to run.\n");
cli_1.Cli.exit(2);
}
if (flagpole_execution_1.FlagpoleExecution.global.volume >= verbosity_1.lineToVerbosity.decoration &&
flagpole_execution_1.FlagpoleExecution.global.isConsoleOutput) {
cli_ansi_1.default.writeLine();
const spinner = cli_1.Cli.instance.spinner(`Loading ${runner.suites.length} test suites...`);
runner.subscribe((message) => {
spinner.updateMessage(message);
});
runner.after(() => {
spinner.stop();
cli_ansi_1.default.write(cli_ansi_1.default.eraseLines(2));
});
}
else {
runner.subscribe((message) => {
cli_ansi_1.default.writeLine(cli_ansi_1.default.cursorUp(), cli_ansi_1.default.eraseLine(), message);
});
}
}
if (!keepCache) {
flagpole_execution_1.FlagpoleExecution.global.clearCache();
}
yield runner.runSpawn(asyncExecution);
cli_1.Cli.exit(runner.exitCode);
});
//# sourceMappingURL=run.js.map