Skip to content

Commit

Permalink
Merge pull request #20 from expressots/18-create-a-method-to-provide-…
Browse files Browse the repository at this point in the history
…an-interface-to-supertest

feat: returns httpServer for e2e testing with supertest
  • Loading branch information
rsaz committed Apr 26, 2024
2 parents 48ada7b + 97999fb commit c2b5009
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/adapter-express/application-express.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import express from "express";
import { IApplicationMessageToConsole, RenderTemplateOptions } from "@expressots/core";
import { ServerEnvironment } from "./application-express.types";

Expand Down Expand Up @@ -29,4 +30,10 @@ export interface IWebServerPublic {
* This includes the extension name, view path, and the engine function itself.
*/
setEngine<T extends RenderTemplateOptions>(options: T): void;

/**
* Get the underlying HTTP server. (default: Express.js)
* @returns The underlying HTTP server after initialization.
*/
getHttpServer(): Promise<express.Application>;
}
21 changes: 19 additions & 2 deletions src/adapter-express/application-express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import { Container } from "inversify";
import { provide } from "inversify-binding-decorators";
import process from "process";
import { ApplicationBase } from "./application-express.base";
import { InversifyExpressServer } from "./express-utils/inversify-express-server";
import {
ExpressHandler,
ExpressoMiddleware,
IWebServer,
MiddlewareConfig,
ServerEnvironment,
} from "./application-express.types";
import { InversifyExpressServer } from "./express-utils/inversify-express-server";

/**
* The AppExpress class provides methods for configuring and running an Express application.
Expand All @@ -33,6 +33,7 @@ import {
*/
@provide(AppExpress)
class AppExpress extends ApplicationBase implements IWebServer {
private logger: Logger = new Logger();
private app: express.Application;
private port: number;
private environment: ServerEnvironment;
Expand Down Expand Up @@ -142,7 +143,7 @@ class AppExpress extends ApplicationBase implements IWebServer {
): Promise<void> {
await this.init();

this.port = port;
this.port = port || 3000;
this.environment = environment;
this.app.set("env", environment);

Expand Down Expand Up @@ -207,6 +208,22 @@ class AppExpress extends ApplicationBase implements IWebServer {
.error("isDevelopment() method must be called on `PostServerInitialization`", "application");
return false;
}

/**
* Get the underlying HTTP server. (default: Express.js)
* @returns The underlying HTTP server after initialization.
*/
public async getHttpServer(): Promise<express.Application> {
if (!this.app) {
this.logger.error(
"The method can only be called in `app.provider` or in e2e tests with supertest.",
"adapter-express",
);

throw new Error("Incorrect usage of `getHttpServer` method");
}
return this.app;
}
}

export { AppExpress };

0 comments on commit c2b5009

Please sign in to comment.