All files / src/__tests__ server.ts

94.73% Statements 18/19
100% Branches 0/0
80% Functions 4/5
100% Lines 18/18

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 43 44 451x 1x                           1x 1x 1x   1x 1x 1x   1x 1x 1x   1x 1x   1x 1x     1x   1x       1x        
import express, { Application } from "express";
import { createServer } from "http";
import type { AddressInfo } from "net";
 
export type MockServer = {
  url: string;
  app: Application;
 
  close(): void;
};
 
/**
 *
 * @return {Promise<MockServer>}
 */
export async function createMockServer(): Promise<MockServer> {
  const app = express();
  const server = createServer(app);
  // eslint-disable-next-line unicorn/consistent-function-scoping
  let onListening: CallableFunction = () => void 0;
  const whenReady = new Promise((resolve) => {
    onListening = resolve;
  });
  const host = "localhost";
  let port = 0;
  let url = "";
 
  server.listen(port, host, () => {
    port = (server.address() as AddressInfo).port;
 
    url = `http://${host}:${port}`;
    onListening();
  });
 
  await whenReady;
 
  return {
    app,
    url,
    close() {
      server.close();
    }
  };
}