UNPKG

mockzilla

Version:

A mocking toolkit leveraging the power of TypeScript to enhance your jest experience.

33 lines (32 loc) 1.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MockzillaError = exports.colorizeStack = exports.getCleanStack = void 0; const jest_message_util_1 = require("jest-message-util"); const jest_validate_1 = require("jest-validate"); const VALID_STACK_LINE = /^\s*at .* \((.+):([0-9]+):([0-9]+)\)$/; const VALID_STACK_LINE2 = /^\s*at (.+):([0-9]+):([0-9]+)$/; function getCleanStack() { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const stack = new Error().stack.split("\n"); const firstValidLine = stack.findIndex((line) => !line.includes("mockzilla") && (VALID_STACK_LINE.test(line) || VALID_STACK_LINE2.test(line))); if (firstValidLine !== -1) return `\n${stack.slice(firstValidLine).join("\n")}`; return "Error analyzing stack trace"; } exports.getCleanStack = getCleanStack; function colorizeStack(stack, noStackTrace = false) { return (0, jest_message_util_1.formatStackTrace)(` \n${stack}`, { rootDir: "", testMatch: [], }, { noStackTrace, }); } exports.colorizeStack = colorizeStack; class MockzillaError extends jest_validate_1.ValidationError { constructor(message, excludeStack) { super("", ""); this.message = excludeStack ? message : `${message}\n\nInvocation: \n${colorizeStack(getCleanStack())}\n`; } } exports.MockzillaError = MockzillaError;