UNPKG

aft-mocha-reporter

Version:

Automated Functional Testing (AFT) Reporter for use with Mocha Test Framework

79 lines 3.9 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.AftMochaReporter = void 0; const Mocha = require("mocha"); const aft_mocha_test_1 = require("./aft-mocha-test"); const aft_core_1 = require("aft-core"); const { EVENT_RUN_BEGIN, EVENT_TEST_FAIL, EVENT_TEST_PASS, EVENT_TEST_PENDING } = Mocha.Runner.constants; /** * this reporter integrates the Automated Functional Testing (AFT) * library into Mocha */ class AftMochaReporter extends Mocha.reporters.Base { constructor(runner) { super(runner); this.addListeners(runner); } get aftCfg() { if (!this._aftCfg) { this._aftCfg = aft_core_1.aftConfig; } return this._aftCfg; } set aftCfg(aftCfg) { this._aftCfg = aftCfg; } addListeners(runner) { /** * NOTE: Mocha events happen out of order for async tests so we cannot rely on * `EVENT_TEST_BEGIN` and run `AftMochaTest.shouldRun` to skip test before entering * the `it` function */ runner .on(EVENT_RUN_BEGIN, () => { // clear all previously cached test results aft_core_1.FileSystemMap.removeMapFile(aft_mocha_test_1.AftMochaTest.name); }) .on(EVENT_TEST_PENDING, (test) => __awaiter(this, void 0, void 0, function* () { /** * conditionally handle `pending` test when not using aftMochaTest * NOTE: always handles when test is manually skipped using `xit` or `xdescribe` */ const t = new aft_mocha_test_1.AftMochaTest({ test }, null, { _preventCacheClear: true }); if (t.results.length === 0) { yield aft_core_1.Err.handleAsync(() => t.pending(), { errLevel: 'none' }); } })) .on(EVENT_TEST_PASS, (test) => __awaiter(this, void 0, void 0, function* () { // conditionally handle `passing` test when not using aftMochaTest const t = new aft_mocha_test_1.AftMochaTest({ test }, null, { _preventCacheClear: true }); if (t.results.length === 0) { yield aft_core_1.Err.handleAsync(() => t.pass(), { errLevel: 'none' }); } })) .on(EVENT_TEST_FAIL, (test, err) => __awaiter(this, void 0, void 0, function* () { var _a; // conditionally handle `failing` test when not using aftMochaTest const t = new aft_mocha_test_1.AftMochaTest({ test }, null, { _preventCacheClear: true }); if (t.results.length === 0) { if (typeof err !== 'string') { const handled = aft_core_1.Err.handle(() => JSON.stringify(err)); err = (_a = handled === null || handled === void 0 ? void 0 : handled.result) !== null && _a !== void 0 ? _a : handled === null || handled === void 0 ? void 0 : handled.message; } yield aft_core_1.Err.handleAsync(() => t.fail(err), { errLevel: 'none' }); } })); } } exports.AftMochaReporter = AftMochaReporter; module.exports = AftMochaReporter; // eslint-disable-line no-undef //# sourceMappingURL=aft-mocha-reporter.js.map