UNPKG

@the-standard/jest-extensions

Version:

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

211 lines (210 loc) 14 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()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); var exceptions_1 = require("@the-standard/exceptions"); var assertion_result_1 = require("../../../models/assertion-result/assertion-result"); var jest_exception_extensions_service_1 = require("./jest-exception-extensions-service"); describe('Jest Exception Extensions Service Test Suite', function () { var service = new jest_exception_extensions_service_1.JestExceptionExtensionsService(); describe('assertActionThrowsExpectedException', function () { test('Should return a passing assertion when the action throws the expected exception', function () { var action = jest.fn(function () { throw new exceptions_1.Exception(); }); var expectedException = new exceptions_1.Exception(); var expectedAssertionResult = new assertion_result_1.AssertionResult('', true); var actualAssertionResult = service.assertActionThrowsExpectedException(action, expectedException); expect(actualAssertionResult).toEqual(expectedAssertionResult); expect(action).toBeCalledTimes(1); }); test('Should return a failing assertion when the action throws an unequivalent exception', function () { var action = jest.fn(function () { throw new exceptions_1.Exception(); }); var expectedException = new exceptions_1.Exception('A message', exceptions_1.Exception.fromError(new Error()), new Map([['key', ['some data']]])); expectedException.name = 'RandomException'; var expectedAssertionResult = new assertion_result_1.AssertionResult([ 'Expected exception name to be "RandomException", was "Exception".', 'Expected exception message to be "A message", was "".', '- Expected map item count to be 1, but found 0.', "- Expected to find key 'key'.", 'Expected an inner exception of type [Error].', ].join('\n'), false); var actualAssertionResult = service.assertActionThrowsExpectedException(action, expectedException); expect(actualAssertionResult).toEqual(expectedAssertionResult); expect(action).toBeCalledTimes(1); }); test('Should return a failing assertion when the action does not throw an exception', function () { var action = jest.fn(function () { }); var expectedException = new exceptions_1.Exception(); var expectedAssertionResult = new assertion_result_1.AssertionResult('Expected action to throw an exception.', false); var actualAssertionResult = service.assertActionThrowsExpectedException(action, expectedException); expect(actualAssertionResult).toEqual(expectedAssertionResult); expect(action).toBeCalledTimes(1); }); test('Should return a failing assertion when the action is not a function', function () { var action = 'a value'; var expectedException = new exceptions_1.Exception(); var expectedAssertionResult = new assertion_result_1.AssertionResult("Expected 'action' to be a function.", false); var actualAssertionResult = service.assertActionThrowsExpectedException(action, expectedException); expect(actualAssertionResult).toEqual(expectedAssertionResult); }); test('Should return a failing assertion when the expected exception is not an exception', function () { var action = function () { throw new exceptions_1.Exception(); }; var expectedException = 'a value'; var expectedAssertionResult = new assertion_result_1.AssertionResult("Expected 'expectedException' to be an Exception.", false); var actualAssertionResult = service.assertActionThrowsExpectedException(action, expectedException); expect(actualAssertionResult).toEqual(expectedAssertionResult); }); test('Should return a failing assertion when the result from the action is a promise', function () { var action = function () { return Promise.resolve(); }; var expectedException = new exceptions_1.Exception(); var expectedAssertionResult = 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); var actualAssertionResult = service.assertActionThrowsExpectedException(action, expectedException); expect(actualAssertionResult).toEqual(expectedAssertionResult); }); }); describe('assertActionThrowsExpectedExceptionAsync', function () { test('Should return a passing assertion when the action throws the expected exception', function () { return __awaiter(void 0, void 0, void 0, function () { var action, expectedException, expectedAssertionResult, actualAssertionResult; return __generator(this, function (_a) { switch (_a.label) { case 0: action = jest.fn(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { throw new exceptions_1.Exception(); }); }); }); expectedException = new exceptions_1.Exception(); expectedAssertionResult = new assertion_result_1.AssertionResult('', true); return [4 /*yield*/, service.assertActionThrowsExpectedExceptionAsync(action, expectedException)]; case 1: actualAssertionResult = _a.sent(); expect(actualAssertionResult).toEqual(expectedAssertionResult); expect(action).toBeCalledTimes(1); return [2 /*return*/]; } }); }); }); test('Should return a failing assertion when the action throws an unequivalent exception', function () { return __awaiter(void 0, void 0, void 0, function () { var action, expectedException, expectedAssertionResult, actualAssertionResult; return __generator(this, function (_a) { switch (_a.label) { case 0: action = jest.fn(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { throw new exceptions_1.Exception(); }); }); }); expectedException = new exceptions_1.Exception('A message', exceptions_1.Exception.fromError(new Error()), new Map([['key', ['some data']]])); expectedException.name = 'RandomException'; expectedAssertionResult = new assertion_result_1.AssertionResult([ 'Expected exception name to be "RandomException", was "Exception".', 'Expected exception message to be "A message", was "".', '- Expected map item count to be 1, but found 0.', "- Expected to find key 'key'.", 'Expected an inner exception of type [Error].', ].join('\n'), false); return [4 /*yield*/, service.assertActionThrowsExpectedExceptionAsync(action, expectedException)]; case 1: actualAssertionResult = _a.sent(); expect(actualAssertionResult).toEqual(expectedAssertionResult); expect(action).toBeCalledTimes(1); return [2 /*return*/]; } }); }); }); test('Should return a failing assertion when the action does not throw an exception', function () { return __awaiter(void 0, void 0, void 0, function () { var action, expectedException, expectedAssertionResult, actualAssertionResult; return __generator(this, function (_a) { switch (_a.label) { case 0: action = jest.fn(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/]; }); }); }); expectedException = new exceptions_1.Exception(); expectedAssertionResult = new assertion_result_1.AssertionResult('Expected action to throw an exception.', false); return [4 /*yield*/, service.assertActionThrowsExpectedExceptionAsync(action, expectedException)]; case 1: actualAssertionResult = _a.sent(); expect(actualAssertionResult).toEqual(expectedAssertionResult); expect(action).toBeCalledTimes(1); return [2 /*return*/]; } }); }); }); test('Should return a failing assertion when the action is not a function', function () { return __awaiter(void 0, void 0, void 0, function () { var action, expectedException, expectedAssertionResult, actualAssertionResult; return __generator(this, function (_a) { switch (_a.label) { case 0: action = 'a value'; expectedException = new exceptions_1.Exception(); expectedAssertionResult = new assertion_result_1.AssertionResult("Expected 'action' to be a function.", false); return [4 /*yield*/, service.assertActionThrowsExpectedExceptionAsync(action, expectedException)]; case 1: actualAssertionResult = _a.sent(); expect(actualAssertionResult).toEqual(expectedAssertionResult); return [2 /*return*/]; } }); }); }); test('Should return a failing assertion when the expected exception is not an exception', function () { return __awaiter(void 0, void 0, void 0, function () { var action, expectedException, expectedAssertionResult, actualAssertionResult; return __generator(this, function (_a) { switch (_a.label) { case 0: action = function () { throw new exceptions_1.Exception(); }; expectedException = 'a value'; expectedAssertionResult = new assertion_result_1.AssertionResult("Expected 'expectedException' to be an Exception.", false); return [4 /*yield*/, service.assertActionThrowsExpectedExceptionAsync(action, expectedException)]; case 1: actualAssertionResult = _a.sent(); expect(actualAssertionResult).toEqual(expectedAssertionResult); return [2 /*return*/]; } }); }); }); }); });