From cf6fdb90eb69bf5143b233ca177d0b5223359ef7 Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Tue, 9 Mar 2021 16:33:39 -0700 Subject: [PATCH] refactor: create test/utils --- test/integration.ts | 21 -------------------- test/{ => utils}/constants.ts | 1 + test/{ => utils}/cssStub.ts | 0 test/{ => utils}/globalSetup.ts | 4 ++-- test/{ => utils}/helpers.ts | 0 test/{ => utils}/httpserver.ts | 6 +++--- test/utils/wtfnode.ts | 35 +++++++++++++++++++++++++++++++++ 7 files changed, 41 insertions(+), 26 deletions(-) delete mode 100644 test/integration.ts rename test/{ => utils}/constants.ts (80%) rename test/{ => utils}/cssStub.ts (100%) rename test/{ => utils}/globalSetup.ts (87%) rename test/{ => utils}/helpers.ts (100%) rename test/{ => utils}/httpserver.ts (95%) create mode 100644 test/utils/wtfnode.ts diff --git a/test/integration.ts b/test/integration.ts deleted file mode 100644 index f829fe5de..000000000 --- a/test/integration.ts +++ /dev/null @@ -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] -} diff --git a/test/constants.ts b/test/utils/constants.ts similarity index 80% rename from test/constants.ts rename to test/utils/constants.ts index ac2250e1c..9a7508922 100644 --- a/test/constants.ts +++ b/test/utils/constants.ts @@ -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" diff --git a/test/cssStub.ts b/test/utils/cssStub.ts similarity index 100% rename from test/cssStub.ts rename to test/utils/cssStub.ts diff --git a/test/globalSetup.ts b/test/utils/globalSetup.ts similarity index 87% rename from test/globalSetup.ts rename to test/utils/globalSetup.ts index 5ef45faac..36b242d7d 100644 --- a/test/globalSetup.ts +++ b/test/utils/globalSetup.ts @@ -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.") } diff --git a/test/helpers.ts b/test/utils/helpers.ts similarity index 100% rename from test/helpers.ts rename to test/utils/helpers.ts diff --git a/test/httpserver.ts b/test/utils/httpserver.ts similarity index 95% rename from test/httpserver.ts rename to test/utils/httpserver.ts index 399ccda11..a66bbbff1 100644 --- a/test/httpserver.ts +++ b/test/utils/httpserver.ts @@ -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 { diff --git a/test/utils/wtfnode.ts b/test/utils/wtfnode.ts new file mode 100644 index 000000000..2d31a4e66 --- /dev/null +++ b/test/utils/wtfnode.ts @@ -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() +}