UNPKG

@assertive-ts/core

Version:

A type-safe fluent assertion library

158 lines 5.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FunctionAssertion = void 0; const tslib_1 = require("tslib"); const es6_1 = tslib_1.__importDefault(require("fast-deep-equal/es6")); const Assertion_1 = require("./Assertion"); const ErrorAssertion_1 = require("./ErrorAssertion"); const messages_1 = require("./helpers/messages"); const assert_1 = require("assert"); /** * Helper symbol used to indicate that no error was captured during * the assertion. * * @hidden */ const NoThrow = Symbol("NoThrow"); /** * Encapsulates assertion methods applicable to functions. * * @param T the type of the function's signature */ class FunctionAssertion extends Assertion_1.Assertion { constructor(actual) { super(actual); } /** * Check if the function throws when called. Optionally, you can check that * the thrown error is strictly equal to an `Error` instance by passing it as * a parameter. * * @example * ``` * expect(throwingFunction).toThrow(); * expect(throwingFunction).toThrow(myErrorInstance); * ``` * * @param error the error the function should throw * @returns the assertion instance */ toThrow(error) { const captured = this.captureError(); if (error !== undefined) { return this.execute({ assertWhen: (0, es6_1.default)(captured, error), error: new assert_1.AssertionError({ actual: captured, expected: error, message: `Expected the function to throw - ${(0, messages_1.prettify)(error)}`, }), invertedError: new assert_1.AssertionError({ actual: this.actual, message: `Expected the function NOT to throw - ${(0, messages_1.prettify)(error)}`, }), }); } return this.execute({ assertWhen: captured !== NoThrow, error: new assert_1.AssertionError({ actual: captured, message: "Expected the function to throw when called", }), invertedError: new assert_1.AssertionError({ actual: captured, message: "Expected the function NOT to throw when called", }), }); } toThrowError(Expected) { const captured = this.captureError(); if (captured === NoThrow) { throw new assert_1.AssertionError({ actual: captured, message: "Expected the function to throw when called", }); } const ErrorType = Expected !== null && Expected !== void 0 ? Expected : Error; const error = new assert_1.AssertionError({ actual: captured, message: `Expected the function to throw an error instance of <${ErrorType.name}>`, }); const invertedError = new assert_1.AssertionError({ actual: captured, message: `Expected the function NOT to throw an error instance of <${ErrorType.name}>`, }); this.execute({ assertWhen: captured instanceof ErrorType, error, invertedError, }); return new ErrorAssertion_1.ErrorAssertion(captured); } /** * Check if the function throws a non-error value when called. Additionally, * you can pass a {@link TypeFactory} in the second argument so the returned * assertion is for the specific value type. Otherwise, a basic * {@link Assertion Assertion<unknown>} instance is returned. * * @example * ``` * expect(raiseValue) * .toThrowValue() * .toBeEqual(someValue); * * expect(raiseExitCode) * .toThrowValue(TypeFactories.Number) * .toBeNegative(); * ``` * * @typeParam S the type of the factory's value * @typeParam A the type of the assertion factory * @param expected the value the function is expected to throw * @param typeFactory optional type factory to perform more specific * assertions over the thrown value * @returns the factory assertion or a basic assertion instance */ toThrowValue(typeFactory) { var _a; const captured = this.captureError(); if (captured === NoThrow) { throw new assert_1.AssertionError({ actual: captured, message: "Expected the function to throw a value", }); } const error = new assert_1.AssertionError({ actual: captured, message: typeFactory ? `Expected the function to throw a value of type "${typeFactory.typeName}"` : "Expected the function to throw a value", }); const invertedError = new assert_1.AssertionError({ actual: captured, message: typeFactory ? `Expected the function NOT to throw a value of type "${typeFactory.typeName}"` : "Expected the function NOT to throw a value", }); const isTypeMatch = (_a = typeFactory === null || typeFactory === void 0 ? void 0 : typeFactory.predicate(captured)) !== null && _a !== void 0 ? _a : true; this.execute({ assertWhen: captured !== NoThrow && isTypeMatch, error, invertedError, }); return (typeFactory === null || typeFactory === void 0 ? void 0 : typeFactory.predicate(captured)) ? new typeFactory.Factory(captured) : new Assertion_1.Assertion(captured); } captureError() { try { this.actual(); return NoThrow; } catch (error) { return error; } } } exports.FunctionAssertion = FunctionAssertion; //# sourceMappingURL=FunctionAssertion.js.map