Skip to content

Commit 0944056

Browse files
committed
feat: add tests for src/node/app.ts
This adds a couple tests for ensureAddress.
1 parent 8a1c129 commit 0944056

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/node/app.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ export const createApp = async (args: DefaultedArgs): Promise<[Express, Express,
6969
export const ensureAddress = (server: http.Server): string => {
7070
const addr = server.address()
7171
if (!addr) {
72-
throw new Error("server has no address")
72+
throw new Error("server has no address") // NOTE@jsjoeio test this line
7373
}
7474
if (typeof addr !== "string") {
7575
return `http://${addr.address}:${addr.port}`
7676
}
77-
return addr
77+
return addr // NOTE@jsjoeio test this line
7878
}

test/unit/node/app.test.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as http from "http"
2+
import { ensureAddress } from "../../../src/node/app"
3+
import { getAvailablePort } from "../../utils/helpers"
4+
5+
describe("ensureAddress", () => {
6+
let mockServer: http.Server
7+
8+
beforeEach(() => {
9+
mockServer = http.createServer()
10+
})
11+
12+
afterEach(() => {
13+
mockServer.close()
14+
})
15+
16+
it("should throw and error if no address", () => {
17+
expect(() => ensureAddress(mockServer)).toThrow("server has no address")
18+
})
19+
it("should return the address if it exists and not a string", async () => {
20+
const port = await getAvailablePort()
21+
mockServer.listen(port)
22+
const address = ensureAddress(mockServer)
23+
expect(address).toBe(`http://:::${port}`)
24+
})
25+
it("should return the address if it exists", async () => {
26+
mockServer.address = () => "http://localhost:8080"
27+
const address = ensureAddress(mockServer)
28+
expect(address).toBe(`http://localhost:8080`)
29+
})
30+
})

0 commit comments

Comments
 (0)