feat: add tests for src/node/app.ts

This adds a couple tests for ensureAddress.
This commit is contained in:
Joe Previte 2021-09-03 11:41:42 -07:00
parent 8a1c129dd1
commit 09440563ca
No known key found for this signature in database
GPG Key ID: 2C91590C6B742C24
2 changed files with 32 additions and 2 deletions

View File

@ -69,10 +69,10 @@ export const createApp = async (args: DefaultedArgs): Promise<[Express, Express,
export const ensureAddress = (server: http.Server): string => { export const ensureAddress = (server: http.Server): string => {
const addr = server.address() const addr = server.address()
if (!addr) { 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") { if (typeof addr !== "string") {
return `http://${addr.address}:${addr.port}` return `http://${addr.address}:${addr.port}`
} }
return addr return addr // NOTE@jsjoeio test this line
} }

View File

@ -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`)
})
})