From 09440563ca53d2590c39b0891aae79da810e6778 Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Fri, 3 Sep 2021 11:41:42 -0700 Subject: [PATCH] feat: add tests for src/node/app.ts This adds a couple tests for ensureAddress. --- src/node/app.ts | 4 ++-- test/unit/node/app.test.ts | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 test/unit/node/app.test.ts diff --git a/src/node/app.ts b/src/node/app.ts index 97ad62c3f..ab185e40a 100644 --- a/src/node/app.ts +++ b/src/node/app.ts @@ -69,10 +69,10 @@ export const createApp = async (args: DefaultedArgs): Promise<[Express, Express, export const ensureAddress = (server: http.Server): string => { const addr = server.address() if (!addr) { - throw new Error("server has no address") + throw new Error("server has no address") // NOTE@jsjoeio test this line } if (typeof addr !== "string") { return `http://${addr.address}:${addr.port}` } - return addr + return addr // NOTE@jsjoeio test this line } diff --git a/test/unit/node/app.test.ts b/test/unit/node/app.test.ts new file mode 100644 index 000000000..41b5515a7 --- /dev/null +++ b/test/unit/node/app.test.ts @@ -0,0 +1,30 @@ +import * as http from "http" +import { ensureAddress } from "../../../src/node/app" +import { getAvailablePort } from "../../utils/helpers" + +describe("ensureAddress", () => { + let mockServer: http.Server + + beforeEach(() => { + mockServer = http.createServer() + }) + + afterEach(() => { + mockServer.close() + }) + + it("should throw and error if no address", () => { + expect(() => ensureAddress(mockServer)).toThrow("server has no address") + }) + it("should return the address if it exists and not a string", async () => { + const port = await getAvailablePort() + mockServer.listen(port) + const address = ensureAddress(mockServer) + expect(address).toBe(`http://:::${port}`) + }) + it("should return the address if it exists", async () => { + mockServer.address = () => "http://localhost:8080" + const address = ensureAddress(mockServer) + expect(address).toBe(`http://localhost:8080`) + }) +})