UNPKG

@the-standard/jest-extensions

Version:

Extensions for jest that provide expect extensions for the-standard libraries

67 lines (66 loc) 3.45 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.JestExceptionExtensionsService = void 0; const exceptions_1 = require("@the-standard/exceptions"); const assertion_result_1 = require("../../../models/assertion-result/assertion-result"); class JestExceptionExtensionsService { assertActionThrowsExpectedException(action, expectedException) { if (typeof action !== 'function') { return this.createAssertionResultForInvalidAction(); } if (!(expectedException instanceof exceptions_1.Exception)) { return this.createAssertionResultForInvalidException(); } try { const result = action(); if (result instanceof Promise) { return new assertion_result_1.AssertionResult("The action passed to 'expect(action).toThrowException(expectedException) returns a promise. Please use the pattern expect(action).toThrowExceptionAsync(expectedException) for async actions.'", false); } return this.createAssertionResultWhenActionDoesNotThrow(); } catch (error) { return this.compareExceptions(error, expectedException); } } createAssertionResultForInvalidAction() { return new assertion_result_1.AssertionResult("Expected 'action' to be a function.", false); } createAssertionResultForInvalidException() { return new assertion_result_1.AssertionResult("Expected 'expectedException' to be an Exception.", false); } createAssertionResultWhenActionDoesNotThrow() { return new assertion_result_1.AssertionResult('Expected action to throw an exception.', false); } compareExceptions(error, expectedException) { const caughtException = exceptions_1.Exception.fromError(error); const [equal, details] = caughtException.equalsWithDetails(expectedException); return new assertion_result_1.AssertionResult(details, equal); } assertActionThrowsExpectedExceptionAsync(asyncAction, expectedException) { return __awaiter(this, void 0, void 0, function* () { if (typeof asyncAction !== 'function') { return this.createAssertionResultForInvalidAction(); } if (!(expectedException instanceof exceptions_1.Exception)) { return this.createAssertionResultForInvalidException(); } try { yield asyncAction(); return this.createAssertionResultWhenActionDoesNotThrow(); } catch (error) { return this.compareExceptions(error, expectedException); } }); } } exports.JestExceptionExtensionsService = JestExceptionExtensionsService;