UNPKG

inversify-express-utils

Version:

Some utilities for the development of express applications with Inversify

110 lines 5.81 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); 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 __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); /* eslint-disable @typescript-eslint/no-unused-vars */ const globals_1 = require("@jest/globals"); const express_1 = __importStar(require("express")); const inversify_1 = require("inversify"); const decorators_1 = require("../decorators"); const httpResponseMessage_1 = require("../httpResponseMessage"); const server_1 = require("../server"); const utils_1 = require("../utils"); (0, globals_1.describe)('Unit Test: InversifyExpressServer', () => { (0, globals_1.beforeEach)(() => { (0, utils_1.cleanUpMetadata)(); }); (0, globals_1.it)('should call the configFn before the errorConfigFn', () => { const middleware = (_req, _res, _next) => undefined; const configFn = globals_1.jest.fn((app) => { app.use(middleware); }); const errorConfigFn = globals_1.jest.fn((app) => { app.use(middleware); }); const container = new inversify_1.Container(); let TestController = class TestController { }; TestController = __decorate([ (0, decorators_1.controller)('/') ], TestController); const server = new server_1.InversifyExpressServer(container); server.setConfig(configFn).setErrorConfig(errorConfigFn); (0, globals_1.expect)(configFn).not.toHaveBeenCalled(); (0, globals_1.expect)(errorConfigFn).not.toHaveBeenCalled(); server.build(); (0, globals_1.expect)(configFn).toHaveBeenCalledTimes(1); (0, globals_1.expect)(errorConfigFn).toHaveBeenCalledTimes(1); }); (0, globals_1.it)('Should allow to pass a custom Router instance as config', () => { const container = new inversify_1.Container(); const customRouter = (0, express_1.Router)({ caseSensitive: false, mergeParams: false, strict: false, }); const serverWithDefaultRouter = new server_1.InversifyExpressServer(container); const serverWithCustomRouter = new server_1.InversifyExpressServer(container, customRouter); (0, globals_1.expect)(serverWithDefaultRouter._router === customRouter).toBe(false); (0, globals_1.expect)(serverWithCustomRouter._router === customRouter).toBe(true); }); (0, globals_1.it)('Should allow to provide custom routing configuration', () => { const container = new inversify_1.Container(); // eslint-disable-next-line @typescript-eslint/typedef const routingConfig = { rootPath: '/such/root/path', }; const serverWithDefaultConfig = new server_1.InversifyExpressServer(container); const serverWithCustomConfig = new server_1.InversifyExpressServer(container, null, routingConfig); (0, globals_1.expect)(serverWithCustomConfig._routingConfig).toBe(routingConfig); (0, globals_1.expect)(serverWithDefaultConfig._routingConfig).not.toEqual(serverWithCustomConfig._routingConfig); }); (0, globals_1.it)('Should allow to provide a custom express application', () => { const container = new inversify_1.Container(); const app = (0, express_1.default)(); const serverWithDefaultApp = new server_1.InversifyExpressServer(container); const serverWithCustomApp = new server_1.InversifyExpressServer(container, null, null, app); (0, globals_1.expect)(serverWithCustomApp._app).toBe(app); (0, globals_1.expect)(serverWithDefaultApp._app).not.toEqual(serverWithCustomApp._app); }); (0, globals_1.it)('Should handle a HttpResponseMessage that has no content', () => { const container = new inversify_1.Container(); const server = new server_1.InversifyExpressServer(container); const httpResponseMessageWithoutContent = new httpResponseMessage_1.HttpResponseMessage(404); const mockResponse = { sendStatus: globals_1.jest.fn(), }; server.handleHttpResponseMessage(httpResponseMessageWithoutContent, mockResponse); (0, globals_1.expect)(mockResponse.sendStatus).toHaveBeenCalledWith(404); }); }); //# sourceMappingURL=framework.test.js.map