UNPKG

earljs

Version:

Ergonomic, modern and type-safe assertion library

64 lines (63 loc) 2.3 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const debug_1 = __importDefault(require("debug")); const testRunnerCtx_1 = require("../testRunnerCtx"); const d = (0, debug_1.default)('earljs:uvu'); const beforeHooks = []; const afterHooks = []; const testRunnerCtx = { testInfo: undefined, beforeTestCase: (fn) => beforeHooks.push(fn), afterTestCase: (fn) => afterHooks.push(fn), }; function main() { if (typeof require !== 'function') { return; } d('earljs/uvu integration is being registered...'); (0, testRunnerCtx_1.setTestRunnerIntegration)(testRunnerCtx); const uvu = require('uvu'); const suite = uvu.suite; uvu.test = wrapTestSuite(uvu.test); uvu.suite = (...args) => wrapTestSuite(suite(...args)); } function wrapTestSuite(test) { const run = wrapTestRun(test, test.run); return new Proxy(test, { get: (target, prop, receiver) => (prop === 'run' ? run : Reflect.get(target, prop, receiver)), apply: (target, thisArg, [name, testCase]) => target.call(thisArg, name, wrapTestCase(testCase)), }); } function wrapTestRun(test, run) { return () => { beforeHooks.forEach((hook) => test.before.each(hook)); afterHooks.forEach((hook) => test.after.each(hook)); return run.call(test); }; } function wrapTestCase(fn) { var _a; // Override the prepareStackTrace function to get access to the unformatted CallSite objects // https://v8.dev/docs/stack-trace-api const { prepareStackTrace } = Error; Error.prepareStackTrace = (_err, stack) => stack; const err = { stack: [] }; Error.captureStackTrace(err, wrapTestCase); const testFilePath = (_a = err.stack[1]) === null || _a === void 0 ? void 0 : _a.getFileName(); Error.prepareStackTrace = prepareStackTrace; if (!testFilePath) { throw new Error('Unable to determine test file path'); } return (ctx) => { testRunnerCtx.testInfo = { suitName: ctx.__suite__ ? [ctx.__suite__] : [], testName: ctx.__test__, testFilePath, }; return fn(ctx); }; } main();