mirror of https://git.tuxpa.in/a/code-server.git
Merge pull request #4099 from cdr/jsjoeio-tests-app
feat: add tests for src/node/app.ts
This commit is contained in:
commit
9c9883e7a6
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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`)
|
||||||
|
})
|
||||||
|
})
|
Loading…
Reference in New Issue