Skip to content

Commit 9c9883e

Browse files
authored
Merge pull request #4099 from cdr/jsjoeio-tests-app
feat: add tests for src/node/app.ts
2 parents 8a1c129 + 0944056 commit 9c9883e

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/node/app.ts

Lines changed: 2 additions & 2 deletions
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

Lines changed: 30 additions & 0 deletions
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)