refactor: create test/utils

This commit is contained in:
Joe Previte 2021-03-09 16:33:39 -07:00
parent b468597872
commit cf6fdb90eb
No known key found for this signature in database
GPG Key ID: 2C91590C6B742C24
7 changed files with 41 additions and 26 deletions

View File

@ -1,21 +0,0 @@
import * as express from "express"
import { createApp } from "../src/node/app"
import { parse, setDefaults, parseConfigFile, DefaultedArgs } from "../src/node/cli"
import { register } from "../src/node/routes"
import * as httpserver from "./httpserver"
export async function setup(
argv: string[],
configFile?: string,
): Promise<[express.Application, express.Application, httpserver.HttpServer, DefaultedArgs]> {
argv = ["--bind-addr=localhost:0", ...argv]
const cliArgs = parse(argv)
const 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]
}

View File

@ -1,3 +1,4 @@
export const CODE_SERVER_ADDRESS = process.env.CODE_SERVER_ADDRESS || "http://localhost:8080"
export const PASSWORD = process.env.PASSWORD || "e45432jklfdsab"
export const STORAGE = process.env.STORAGE || ""
export const E2E_VIDEO_DIR = "./test/e2e/videos"

View File

@ -6,7 +6,7 @@ import { CODE_SERVER_ADDRESS, PASSWORD } from "./constants"
import * as wtfnode from "./wtfnode"
module.exports = async () => {
console.log("\n🚨 Running Global Setup for Jest Tests")
console.log("\n🚨 Running Global Setup for Jest End-to-End Tests")
console.log(" Please hang tight...")
const browser = await chromium.launch()
const context = await browser.newContext()
@ -30,5 +30,5 @@ module.exports = async () => {
await page.close()
await browser.close()
await context.close()
console.log("✅ Global Setup for Jest Tests is now complete.")
console.log("✅ Global Setup for Jest End-to-End Tests is now complete.")
}

View File

@ -3,9 +3,9 @@ import * as http from "http"
import * as net from "net"
import * as nodeFetch from "node-fetch"
import Websocket from "ws"
import * as util from "../src/common/util"
import { ensureAddress } from "../src/node/app"
import { handleUpgrade } from "../src/node/wsRouter"
import * as util from "../../src/common/util"
import { ensureAddress } from "../../src/node/app"
import { handleUpgrade } from "../../src/node/wsRouter"
// Perhaps an abstraction similar to this should be used in app.ts as well.
export class HttpServer {

35
test/utils/wtfnode.ts Normal file
View File

@ -0,0 +1,35 @@
import * as util from "util"
import * as wtfnode from "wtfnode"
// Jest seems to hijack console.log in a way that makes the output difficult to
// read. So we'll write directly to process.stderr instead.
const write = (...args: [any, ...any]) => {
if (args.length > 0) {
process.stderr.write(util.format(...args) + "\n")
}
}
wtfnode.setLogger("info", write)
wtfnode.setLogger("warn", write)
wtfnode.setLogger("error", write)
let active = false
/**
* Start logging open handles periodically. This can be used to see what is
* hanging open if anything.
*/
export function setup(): void {
if (active) {
return
}
active = true
const interval = 5000
const wtfnodeDump = () => {
wtfnode.dump()
const t = setTimeout(wtfnodeDump, interval)
t.unref()
}
const t = setTimeout(wtfnodeDump, interval)
t.unref()
}