UNPKG

@studyportals/sp-r2d2

Version:

A framework that contains various components used when developing projects that will be deployed via AWS λ.

217 lines 11.3 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; 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 }); const mocha_1 = require("@testdeck/mocha"); const Moq = require("typemoq"); const chai_1 = require("chai"); const base_bootstrapper_class_1 = require("../../environment/bootstrapping/base-bootstrapper.class"); let BaseBootstrapperTest = class BaseBootstrapperTest { get processMock() { return this._processMock; } get process() { return this.processMock.object; } get responseSenderMock() { return this._responseSenderMock; } get responseSender() { return this.responseSenderMock.object; } get testInstanceMock() { return this._testInstanceMock; } get testInstance() { return this.testInstanceMock.object; } before() { this._processMock = Moq.Mock.ofType(); this._responseSenderMock = Moq.Mock.ofType(); this._testInstanceMock = Moq.Mock.ofType(base_bootstrapper_class_1.BaseBootstrapper); this.testInstanceMock.callBase = true; this.testInstanceMock.setup((_) => _['responseSender']).returns(() => this.responseSender); this.testInstanceMock.setup((_) => _['process']).returns(() => this.process); } getProcess__Exists() { const testInstance = new base_bootstrapper_class_1.BaseBootstrapper(this.responseSender); chai_1.assert.exists(testInstance['process']); } handleUncaughtError_Error_ErrorCorrectlySent() { return __awaiter(this, void 0, void 0, function* () { const error = new Error(); let wasAwaited = false; this.responseSenderMock .setup((_) => _.sendUncaughtError(Moq.It.isAny())) .returns(() => new Promise((res) => { setTimeout(() => { wasAwaited = true; res(); }, 10); })); yield this.testInstance['handleUncaughtError'](error); this.responseSenderMock.verify((_) => _.sendUncaughtError(error), Moq.Times.once()); chai_1.assert.isTrue(wasAwaited); }); } handleUncaughtError_NotError_SerializedAndSentInNewErrorMessage() { return __awaiter(this, void 0, void 0, function* () { const error = { SomeProperty: 'SomeValue' }; let receivedError; this.responseSenderMock.setup((_) => _.sendUncaughtError(Moq.It.isAny())).callback((_) => (receivedError = _)); yield this.testInstance['handleUncaughtError'](error); chai_1.assert.instanceOf(receivedError, Error); chai_1.assert.equal(receivedError.message, JSON.stringify(error)); }); } installGlobalUncaughtErrorHandlers__UncaughtExceptionListenersRemovedBeforeInstalling() { let callNumber = 0; let removeAllCallNumber = 0; let installCallNumber = 0; this.processMock .setup((_) => _.removeAllListeners('uncaughtException')) .callback(() => (removeAllCallNumber = ++callNumber)); this.processMock .setup((_) => _.on('uncaughtException', Moq.It.isAny())) .callback(() => (installCallNumber = ++callNumber)); this.testInstance['installGlobalUncaughtErrorHandlers'](); chai_1.assert.notEqual(removeAllCallNumber, 0); chai_1.assert.isTrue(installCallNumber >= removeAllCallNumber); } installGlobalUncaughtErrorHandlers__InstalledCorrectUncaughtExceptionHandler() { const error = 'SomeError'; this.testInstanceMock.setup((_) => _['handleUncaughtError'](Moq.It.isAny())); this.processMock.setup((_) => _.on('uncaughtException', Moq.It.isAny())).callback((_, __) => __(error)); this.testInstance['installGlobalUncaughtErrorHandlers'](); this.testInstanceMock.verify((_) => _['handleUncaughtError'](error), Moq.Times.once()); } installGlobalUncaughtErrorHandlers__UnhandledRejectionListenersRemovedBeforeInstalling() { let callNumber = 0; let removeAllCallNumber = 0; let installCallNumber = 0; this.processMock .setup((_) => _.removeAllListeners('unhandledRejection')) .callback(() => (removeAllCallNumber = ++callNumber)); this.processMock .setup((_) => _.on('unhandledRejection', Moq.It.isAny())) .callback(() => (installCallNumber = ++callNumber)); this.testInstance['installGlobalUncaughtErrorHandlers'](); chai_1.assert.notEqual(removeAllCallNumber, 0); chai_1.assert.isTrue(installCallNumber >= removeAllCallNumber); } installGlobalUncaughtErrorHandlers__InstalledCorrectUnhandledRejectionHandler() { const error = 'SomeError'; this.testInstanceMock.setup((_) => _['handleUncaughtError'](Moq.It.isAny())); this.processMock.setup((_) => _.on('unhandledRejection', Moq.It.isAny())).callback((_, __) => __(error)); this.testInstance['installGlobalUncaughtErrorHandlers'](); this.testInstanceMock.verify((_) => _['handleUncaughtError'](error), Moq.Times.once()); } bootstrap__GlobalUncaughtErrorHandlersInstalled() { return __awaiter(this, void 0, void 0, function* () { this.testInstanceMock.setup((_) => _['installGlobalUncaughtErrorHandlers']()); this.testInstanceMock.setup((_) => _['configureInstance']()); this.testInstanceMock.setup((_) => _['configureEnvironment']()); yield this.testInstance.bootstrap(); this.testInstanceMock.verify((_) => _['installGlobalUncaughtErrorHandlers'](), Moq.Times.once()); }); } bootstrap__InstanceCorrectlyConfigured() { return __awaiter(this, void 0, void 0, function* () { let wasAwaited = false; this.testInstanceMock.setup((_) => _['installGlobalUncaughtErrorHandlers']()); this.testInstanceMock .setup((_) => _['configureInstance']()) .returns(() => new Promise((res) => { setTimeout(() => { wasAwaited = true; res(); }, 10); })); this.testInstanceMock.setup((_) => _['configureEnvironment']()); yield this.testInstance.bootstrap(); this.testInstanceMock.verify((_) => _['configureInstance'](), Moq.Times.once()); chai_1.assert.isTrue(wasAwaited); }); } bootstrap_EnvironmentNotConfigured_EnvironmentCorrectlyConfigured() { return __awaiter(this, void 0, void 0, function* () { let wasAwaited = false; this.testInstanceMock.setup((_) => _['installGlobalUncaughtErrorHandlers']()); this.testInstanceMock.setup((_) => _['configureInstance']()); this.testInstanceMock .setup((_) => _['configureEnvironment']()) .returns(() => new Promise((res) => { setTimeout(() => { wasAwaited = true; res(); }, 10); })); base_bootstrapper_class_1.BaseBootstrapper['ENVIRONMENT_BOOTSTRAPPED'] = false; yield this.testInstance.bootstrap(); this.testInstanceMock.verify((_) => _['configureEnvironment'](), Moq.Times.once()); chai_1.assert.isTrue(wasAwaited); }); } bootstrap_EnvironmentNotConfigured_EnvironmentCorrectlyConfiguredOnlyOnceInMultipleCalls() { return __awaiter(this, void 0, void 0, function* () { this.testInstanceMock.setup((_) => _['installGlobalUncaughtErrorHandlers']()); this.testInstanceMock.setup((_) => _['configureInstance']()); this.testInstanceMock.setup((_) => _['configureEnvironment']()); base_bootstrapper_class_1.BaseBootstrapper['ENVIRONMENT_BOOTSTRAPPED'] = false; yield this.testInstance.bootstrap(); yield this.testInstance.bootstrap(); yield this.testInstance.bootstrap(); this.testInstanceMock.verify((_) => _['configureEnvironment'](), Moq.Times.once()); }); } }; __decorate([ mocha_1.test ], BaseBootstrapperTest.prototype, "getProcess__Exists", null); __decorate([ mocha_1.test ], BaseBootstrapperTest.prototype, "handleUncaughtError_Error_ErrorCorrectlySent", null); __decorate([ mocha_1.test ], BaseBootstrapperTest.prototype, "handleUncaughtError_NotError_SerializedAndSentInNewErrorMessage", null); __decorate([ mocha_1.test ], BaseBootstrapperTest.prototype, "installGlobalUncaughtErrorHandlers__UncaughtExceptionListenersRemovedBeforeInstalling", null); __decorate([ mocha_1.test ], BaseBootstrapperTest.prototype, "installGlobalUncaughtErrorHandlers__InstalledCorrectUncaughtExceptionHandler", null); __decorate([ mocha_1.test ], BaseBootstrapperTest.prototype, "installGlobalUncaughtErrorHandlers__UnhandledRejectionListenersRemovedBeforeInstalling", null); __decorate([ mocha_1.test ], BaseBootstrapperTest.prototype, "installGlobalUncaughtErrorHandlers__InstalledCorrectUnhandledRejectionHandler", null); __decorate([ mocha_1.test ], BaseBootstrapperTest.prototype, "bootstrap__GlobalUncaughtErrorHandlersInstalled", null); __decorate([ mocha_1.test ], BaseBootstrapperTest.prototype, "bootstrap__InstanceCorrectlyConfigured", null); __decorate([ mocha_1.test ], BaseBootstrapperTest.prototype, "bootstrap_EnvironmentNotConfigured_EnvironmentCorrectlyConfigured", null); __decorate([ mocha_1.test ], BaseBootstrapperTest.prototype, "bootstrap_EnvironmentNotConfigured_EnvironmentCorrectlyConfiguredOnlyOnceInMultipleCalls", null); BaseBootstrapperTest = __decorate([ (0, mocha_1.suite)() ], BaseBootstrapperTest); //# sourceMappingURL=base-bootstrapper.test.js.map