Skip to content

Commit 88cc005

Browse files
committed
test: Implement integration.ts for near full stack integration testing
1 parent a5ab2d1 commit 88cc005

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

src/node/cli.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ export const optionDescriptions = (): string[] => {
244244
export const parse = (
245245
argv: string[],
246246
opts?: {
247-
configFile: string
247+
configFile?: string
248248
},
249249
): Args => {
250250
const error = (msg: string): Error => {
@@ -521,7 +521,19 @@ export async function readConfigFile(configPath?: string): Promise<ConfigArgs> {
521521
}
522522

523523
const configFile = await fs.readFile(configPath)
524-
const config = yaml.safeLoad(configFile.toString(), {
524+
return parseConfigFile(configFile.toString(), configPath)
525+
}
526+
527+
/**
528+
* parseConfigFile parses configFile into ConfigArgs.
529+
* configPath is used as the filename in error messages
530+
*/
531+
export function parseConfigFile(configFile: string, configPath: string): ConfigArgs {
532+
if (configFile == "") {
533+
return { _: [], config: configPath }
534+
}
535+
536+
const config = yaml.safeLoad(configFile, {
525537
filename: configPath,
526538
})
527539
if (!config || typeof config === "string") {

test/httpserver.ts

+16
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@ import * as nodeFetch from "node-fetch"
33
import * as util from "../src/common/util"
44
import { ensureAddress } from "../src/node/app"
55

6+
// Perhaps an abstraction similar to this should be used in app.ts as well.
67
export class HttpServer {
78
private hs = http.createServer()
89

10+
public constructor(hs?: http.Server) {
11+
// See usage in test/integration.ts
12+
if (hs) {
13+
this.hs = hs
14+
}
15+
}
16+
917
/**
1018
* listen starts the server on a random localhost port.
1119
* Use close to cleanup when done.
@@ -53,4 +61,12 @@ export class HttpServer {
5361
public fetch(requestPath: string, opts?: nodeFetch.RequestInit): Promise<nodeFetch.Response> {
5462
return nodeFetch.default(`${ensureAddress(this.hs)}${requestPath}`, opts)
5563
}
64+
65+
public port(): number {
66+
const addr = this.hs.address()
67+
if (addr && typeof addr == "object") {
68+
return addr.port
69+
}
70+
throw new Error("server not listening or listening on unix socket")
71+
}
5672
}

test/integration.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { createApp } from "../src/node/app"
2+
import { register } from "../src/node/routes"
3+
import { parse, setDefaults, parseConfigFile, DefaultedArgs } from "../src/node/cli"
4+
import * as httpserver from "./httpserver"
5+
import * as express from "express"
6+
7+
export async function setup(argv: string[], configFile?: string): Promise<[express.Application, express.Application, httpserver.HttpServer, DefaultedArgs]> {
8+
const cliArgs = parse(argv)
9+
let configArgs = parseConfigFile(configFile || "", "test/integration.ts")
10+
const args = await setDefaults(cliArgs, configArgs)
11+
12+
const [app, wsApp, server] = await createApp(args)
13+
await register(app, wsApp, server, args)
14+
15+
return [app, wsApp, new httpserver.HttpServer(server), args]
16+
}

0 commit comments

Comments
 (0)