All files / src/app app.ts

0% Statements 0/26
100% Branches 0/0
0% Functions 0/3
0% Lines 0/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42                                                                                   
import path from "path";
import { app } from "../api";
import { registerLegacyRoutes } from "../legacy";
import { logger } from "../logger";
import { internalRouter } from "../routes";
import { MockApiServer } from "../server";
import { coverageService } from "../services";
import { findFilesFromPattern } from "../utils";
import { normalizePath } from "../utils/path-utils";
import { ApiMockAppConfig } from "./config";
 
export const ROUTE_FOLDER = normalizePath(path.join(__dirname, "../test-routes"));
 
export class ApiMockApp {
  private server: MockApiServer;
 
  constructor(private config: ApiMockAppConfig) {
    this.server = new MockApiServer({ port: config.port });
  }
 
  public async start(): Promise<void> {
    this.server.use("/", internalRouter);
 
    await requireMockRoutes(ROUTE_FOLDER);
 
    // Need to init after registering the new routes but before the legacy routes.
    coverageService.init(this.config.coverageDirectory, this.config.appendCoverage);
    registerLegacyRoutes(this.server);
    const apiRouter = app;
    this.server.use("/", apiRouter.router);
    this.server.start();
  }
}
 
export const requireMockRoutes = async (routesFolder: string): Promise<void> => {
  const files = await findFilesFromPattern(normalizePath(path.join(routesFolder, "**/*.js")));
  logger.debug("Detected routes:", files);
  for (const file of files) {
    require(path.resolve(file));
  }
};