UNPKG

earljs

Version:

Ergonomic, modern and type-safe assertion library

98 lines (97 loc) 3.56 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MochaCtx = void 0; const debug_1 = __importDefault(require("debug")); const mocha_1 = __importDefault(require("mocha")); const ts_essentials_1 = require("ts-essentials"); const testRunnerCtx_1 = require("../testRunnerCtx"); const d = (0, debug_1.default)('earljs:mocha'); /** * Needed in Mocha --watch mode. Mocha doesn't export hooks before mocha.ui() is called */ function main() { d('earljs/mocha integration is being registered...'); for (const module of findMochaInstances()) { if (!module || module.__earljs_integrated) { continue; } ; module.__earljs_integrated = true; d('Monkey-patching Mocha.prototype.ui'); const { ui } = module.prototype; module.prototype.ui = function (...args) { (0, testRunnerCtx_1.setTestRunnerIntegration)(new MochaCtx(this.suite)); return ui.apply(this, args); }; } } /** * In mocha run mode, we use the suite with hooks already assigned to Mocha's exports. */ exports.mochaGlobalSetup = function () { d('Integrating earl with mocha...'); if (mocha_1.default.beforeEach) { (0, testRunnerCtx_1.setTestRunnerIntegration)(new MochaCtx(mocha_1.default)); } }; function findMochaInstances() { var _a; const mochaFromWindow = (_a = globalThis.window) === null || _a === void 0 ? void 0 : _a.Mocha; if (mochaFromWindow) { return [mochaFromWindow]; } const req = require; if (typeof req === 'function') { // require can be undefined in Node ESM and browser contexts // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition const cache = req.cache || {}; return Object.keys(cache) .filter(function (child) { var _a; var val = (_a = cache[child]) === null || _a === void 0 ? void 0 : _a.exports; return typeof val === 'function' && val.name === 'Mocha'; }) .map(function (child) { var _a; return (_a = cache[child]) === null || _a === void 0 ? void 0 : _a.exports; }); } return []; } class MochaCtx { constructor(_hooks) { this._hooks = _hooks; const self = this; d('Installing beforeEach hook to get testInfo before each test'); _hooks.beforeEach(function () { (0, ts_essentials_1.assert)(this.currentTest, "Current test not set by mocha. This shouldn't happen."); (0, ts_essentials_1.assert)(this.currentTest.file, "Current test file path not set by mocha. This shouldn't happen."); (0, ts_essentials_1.assert)(this.currentTest.parent, "Current test has no parent set by mocha. This shouldn't happen."); self.testInfo = { suitName: makeSuiteName(this.currentTest.parent), testName: this.currentTest.title, testFilePath: this.currentTest.file, }; }); } afterTestCase(fn) { this._hooks.afterEach(fn); } beforeTestCase(fn) { this._hooks.beforeEach(fn); } } exports.MochaCtx = MochaCtx; function makeSuiteName(testCtx) { if (testCtx.parent) { return [...makeSuiteName(testCtx.parent), testCtx.title]; } if (testCtx.title) { return [testCtx.title]; } return []; } main();