Skip to content

Commit

Permalink
test: unit test the port finding function
Browse files Browse the repository at this point in the history
  • Loading branch information
ludofischer committed Apr 13, 2022
1 parent 983a0aa commit a421822
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions test/server/get-port.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"use strict";

const net = require("net");
const util = require("util");
const getPort = require("../../lib/getPort");

it("it should bind to the preferred port", async () => {
const preferredPort = 8080;
const port = await getPort(8080);
expect(port).toBe(preferredPort);
});

it("should pick the next port if the preferred port is unavailable", async () => {
const preferredPort = 8345;
const server = net.createServer();
server.unref();
await util.promisify(server.listen.bind(server))(preferredPort);
const port = await getPort(preferredPort);
expect(port).toBe(preferredPort + 1);
});

it("should reject privileged ports", async () => {
try {
await getPort(80);
} catch (e) {
expect(e.message).toBeDefined();
}
});

it("should reject too high port numbers", async () => {
try {
await getPort(65536);
} catch (e) {
expect(e.message).toBeDefined();
}
});

0 comments on commit a421822

Please sign in to comment.