From 240c8e266ed0e18517e5c98e0ab270ce63b6e446 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Thu, 14 Jan 2021 10:53:58 -0500 Subject: [PATCH] test: Implement integration.ts for near full stack integration testing --- src/node/cli.ts | 16 ++++++++++++++-- test/httpserver.ts | 16 ++++++++++++++++ test/integration.ts | 16 ++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 test/integration.ts diff --git a/src/node/cli.ts b/src/node/cli.ts index 5a0e4473e..2469bedbf 100644 --- a/src/node/cli.ts +++ b/src/node/cli.ts @@ -244,7 +244,7 @@ export const optionDescriptions = (): string[] => { export const parse = ( argv: string[], opts?: { - configFile: string + configFile?: string }, ): Args => { const error = (msg: string): Error => { @@ -521,7 +521,19 @@ export async function readConfigFile(configPath?: string): Promise { } const configFile = await fs.readFile(configPath) - const config = yaml.safeLoad(configFile.toString(), { + return parseConfigFile(configFile.toString(), configPath) +} + +/** + * parseConfigFile parses configFile into ConfigArgs. + * configPath is used as the filename in error messages + */ +export function parseConfigFile(configFile: string, configPath: string): ConfigArgs { + if (configFile == "") { + return { _: [], config: configPath } + } + + const config = yaml.safeLoad(configFile, { filename: configPath, }) if (!config || typeof config === "string") { diff --git a/test/httpserver.ts b/test/httpserver.ts index dc88da3db..570adf13d 100644 --- a/test/httpserver.ts +++ b/test/httpserver.ts @@ -3,9 +3,17 @@ import * as nodeFetch from "node-fetch" import * as util from "../src/common/util" import { ensureAddress } from "../src/node/app" +// Perhaps an abstraction similar to this should be used in app.ts as well. export class HttpServer { private hs = http.createServer() + public constructor(hs?: http.Server) { + // See usage in test/integration.ts + if (hs) { + this.hs = hs + } + } + /** * listen starts the server on a random localhost port. * Use close to cleanup when done. @@ -53,4 +61,12 @@ export class HttpServer { public fetch(requestPath: string, opts?: nodeFetch.RequestInit): Promise { return nodeFetch.default(`${ensureAddress(this.hs)}${requestPath}`, opts) } + + public port(): number { + const addr = this.hs.address() + if (addr && typeof addr == "object") { + return addr.port + } + throw new Error("server not listening or listening on unix socket") + } } diff --git a/test/integration.ts b/test/integration.ts new file mode 100644 index 000000000..a4cc7586d --- /dev/null +++ b/test/integration.ts @@ -0,0 +1,16 @@ +import { createApp } from "../src/node/app" +import { register } from "../src/node/routes" +import { parse, setDefaults, parseConfigFile, DefaultedArgs } from "../src/node/cli" +import * as httpserver from "./httpserver" +import * as express from "express" + +export async function setup(argv: string[], configFile?: string): Promise<[express.Application, express.Application, httpserver.HttpServer, DefaultedArgs]> { + const cliArgs = parse(argv) + let configArgs = parseConfigFile(configFile || "", "test/integration.ts") + const args = await setDefaults(cliArgs, configArgs) + + const [app, wsApp, server] = await createApp(args) + await register(app, wsApp, server, args) + + return [app, wsApp, new httpserver.HttpServer(server), args] +}