Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/node/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
30 changes: 30 additions & 0 deletions test/unit/node/app.test.ts
Original file line number Diff line number Diff line change
@@ -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`)
})
})