Merge pull request #4099 from cdr/jsjoeio-tests-app

feat: add tests for src/node/app.ts
This commit is contained in:
Joe Previte 2021-09-10 17:03:15 +00:00 committed by GitHub
commit 9c9883e7a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 => {
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
}

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