Skip to content

feat(testing): add test for app > listen #4971

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
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/node/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface App extends Disposable {
server: http.Server
}

const listen = (server: http.Server, { host, port, socket, "socket-mode": mode }: ListenOptions) => {
export const listen = (server: http.Server, { host, port, socket, "socket-mode": mode }: ListenOptions) => {
return new Promise<void>(async (resolve, reject) => {
server.on("error", reject)

Expand Down
35 changes: 33 additions & 2 deletions test/unit/node/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { promises } from "fs"
import * as http from "http"
import * as https from "https"
import * as path from "path"
import { createApp, ensureAddress, handleArgsSocketCatchError, handleServerError } from "../../../src/node/app"
import { createApp, ensureAddress, handleArgsSocketCatchError, handleServerError, listen } from "../../../src/node/app"
import { OptionalString, setDefaults } from "../../../src/node/cli"
import { generateCertificate } from "../../../src/node/util"
import { clean, mockLogger, getAvailablePort, tmpdir } from "../../utils/helpers"
Expand Down Expand Up @@ -219,7 +219,7 @@ describe("handleArgsSocketCatchError", () => {
expect(logger.error).toHaveBeenCalledWith(errorMessage)
})

it("should not log an error if its a iNodeJS.ErrnoException", () => {
it("should not log an error if its a NodeJS.ErrnoException", () => {
const error: NodeJS.ErrnoException = new Error()
error.code = "ENOENT"

Expand Down Expand Up @@ -250,3 +250,34 @@ describe("handleArgsSocketCatchError", () => {
expect(logger.error).toHaveBeenCalledWith(error)
})
})

describe("listen", () => {
let tmpDirPath: string
let mockServer: http.Server

const testName = "listen"

beforeEach(async () => {
await clean(testName)
mockLogger()
tmpDirPath = await tmpdir(testName)
mockServer = http.createServer()
})

afterEach(() => {
mockServer.close()
jest.clearAllMocks()
})

it.only("should log an error if a directory is passed in instead of a file", async () => {
const errorMessage = "EPERM: operation not permitted, unlink"
const port = await getAvailablePort()
const mockArgs = { port, host: "0.0.0.0", socket: tmpDirPath }
try {
await listen(mockServer, mockArgs)
} catch (error) {}

expect(logger.error).toHaveBeenCalledTimes(1)
expect(logger.error).toHaveBeenCalledWith(expect.stringContaining(errorMessage))
})
})