test: Implement integration.ts for near full stack integration testing

This commit is contained in:
Anmol Sethi 2021-01-14 10:53:58 -05:00
parent 64e915de4a
commit 240c8e266e
No known key found for this signature in database
GPG Key ID: 8CEF1878FF10ADEB
3 changed files with 46 additions and 2 deletions

View File

@ -244,7 +244,7 @@ export const optionDescriptions = (): string[] => {
export const parse = ( export const parse = (
argv: string[], argv: string[],
opts?: { opts?: {
configFile: string configFile?: string
}, },
): Args => { ): Args => {
const error = (msg: string): Error => { const error = (msg: string): Error => {
@ -521,7 +521,19 @@ export async function readConfigFile(configPath?: string): Promise<ConfigArgs> {
} }
const configFile = await fs.readFile(configPath) 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, filename: configPath,
}) })
if (!config || typeof config === "string") { if (!config || typeof config === "string") {

View File

@ -3,9 +3,17 @@ import * as nodeFetch from "node-fetch"
import * as util from "../src/common/util" import * as util from "../src/common/util"
import { ensureAddress } from "../src/node/app" import { ensureAddress } from "../src/node/app"
// Perhaps an abstraction similar to this should be used in app.ts as well.
export class HttpServer { export class HttpServer {
private hs = http.createServer() 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. * listen starts the server on a random localhost port.
* Use close to cleanup when done. * Use close to cleanup when done.
@ -53,4 +61,12 @@ export class HttpServer {
public fetch(requestPath: string, opts?: nodeFetch.RequestInit): Promise<nodeFetch.Response> { public fetch(requestPath: string, opts?: nodeFetch.RequestInit): Promise<nodeFetch.Response> {
return nodeFetch.default(`${ensureAddress(this.hs)}${requestPath}`, opts) 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")
}
} }

16
test/integration.ts Normal file
View File

@ -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]
}