UNPKG

@qaflag/core

Version:

Base requirements for the QA Flag library

162 lines 7.3 kB
"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.Suite = exports.CaseDefinitions = exports.ScenarioDefinitions = void 0; const kv_store_1 = require("../models/kv-store"); const logger_1 = require("../models/logger"); const events_1 = require("events"); const persona_1 = require("../persona/persona"); const before_decorator_1 = require("../decorators/before.decorator"); const after_decorator_1 = require("../decorators/after.decorator"); exports.ScenarioDefinitions = Symbol('ScenarioDefinitions'); exports.CaseDefinitions = Symbol('CaseDefinitions'); function Suite(suiteOpts) { return class SuiteAbstract { get defaultScenarioOpts() { return { baseUrl: this.baseUrl, }; } get befores() { return Object.entries(this[before_decorator_1.BeforeSymbol] || {}); } get afters() { return Object.entries(this[after_decorator_1.AfterSymbol] || {}); } constructor(settings) { this.settings = settings; this.title = suiteOpts.title; this.store = new kv_store_1.KvStore(); this.logger = new logger_1.Logger(); this.events = new events_1.EventEmitter(); this.scenarios = []; this.cases = []; this.steps = []; this.persona = suiteOpts.persona || new persona_1.DefaultUser(); this.baseUrl = suiteOpts.baseUrl || settings.baseUrl; const scenarioMethods = this[exports.ScenarioDefinitions]; if (scenarioMethods) { Object.values(scenarioMethods) .sort((a, b) => a.step - b.step) .forEach(scenarioOpts => { const scenarioConstructor = scenarioOpts.type || suiteOpts.type; if (!scenarioConstructor) { throw 'No Scenario Type defined. It must be set either in the Scenario decorator in the Suite decorator, as a default.'; } const scenario = this.addScenarioToStep(new scenarioConstructor(Object.assign(Object.assign({}, this.defaultScenarioOpts), scenarioOpts), this)); this.scenarios.push(scenario); }); } const caseMethods = this[exports.CaseDefinitions]; if (caseMethods) { Object.values(caseMethods).forEach(testCase => this.cases.push(testCase)); } } __execute() { return __awaiter(this, void 0, void 0, function* () { this.logger.start(); yield this.persona.__startUp(this); yield Promise.all(this.scenarios.map(scenario => { var _a; return (_a = scenario.persona) === null || _a === void 0 ? void 0 : _a.__startUp(this); })); this.events.emit('beforeAll'); yield Promise.all(this.befores.map(([methodName, before]) => before(this, this.defaultScenarioOpts))); for (const step of this.steps) { let fatalError = false; yield Promise.all(step.scenarios.map((scenario) => __awaiter(this, void 0, void 0, function* () { try { this.events.emit('beforeEach', scenario); yield scenario.__startUp(); yield scenario.__execute(); try { yield scenario.__next(scenario); } catch (ex) { scenario.logger.fail(`${ex}`); } yield scenario.__tearDown(); this.events.emit('afterEach', scenario); this.logger.log(scenario.status == 'pass' ? 'pass' : 'fail', { text: scenario.title, }); } catch (ex) { scenario.logger.fail(ex); this.logger.fail(scenario.title); fatalError = true; } }))); if (fatalError) break; } yield Promise.all(this.afters.map(([methodName, after]) => after(this, this.defaultScenarioOpts))); this.logger.end(); this.events.emit('completed'); this.events.emit(this.logger.failed ? 'failed' : 'passed'); }); } getStep(stepNumber) { const step = this.steps.find(step => step.stepNumber === stepNumber); if (step) return step; const newStep = { stepNumber, scenarios: [], }; this.steps.push(newStep); this.steps.sort((a, b) => a.stepNumber - b.stepNumber); return newStep; } addScenarioToStep(scenario) { const step = this.getStep(scenario.step); step.scenarios.push(scenario); return scenario; } set(key, value) { return this.store.set(key, value); } get(key) { return this.store.get(key); } push(key, value) { return this.store.push(key, value); } get results() { const results = { status: 'pass', assertions: { passCount: 0, failCount: 0, optionalFailCount: 0, }, scenarios: { passCount: 0, failCount: 0, }, }; this.scenarios.forEach(scenario => { const scenarioResult = scenario.result; results.assertions.passCount += scenarioResult.passCount; results.assertions.failCount += scenarioResult.failCount; results.assertions.optionalFailCount += scenarioResult.optionalFailCount; if (scenarioResult.failCount > 0) { results.status = 'fail'; results.scenarios.failCount++; } else { results.scenarios.passCount++; } }); return results; } }; } exports.Suite = Suite; //# sourceMappingURL=suite.mixin.js.map