Skip to content
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

fix: ranges now work even if port is unset in options #72

Merged
merged 5 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion src/get-port.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function getPort(
_userOptions = { port: Number.parseInt(_userOptions + "") || 0 };
}

const _port = Number(_userOptions.port ?? process.env.PORT ?? 3000);
const _port = Number(_userOptions.port ?? process.env.PORT);

const options = {
name: "default",
Expand Down Expand Up @@ -63,6 +63,9 @@ export async function getPort(
}
return true;
});
if (portsToCheck.length === 0) {
portsToCheck.push(3000);
}

// Try to find a port
let availablePort = await _findPort(portsToCheck, options.host);
Expand Down
11 changes: 9 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("getPort", () => {
portBlocker?.close();
});

describe("default host`", () => {
describe("default host", () => {
test("default port is not in use", async () => {
const port = await getPort();
expect(port).toEqual(3000);
Expand All @@ -26,6 +26,13 @@ describe("getPort", () => {
});
});

describe("order", () => {
test("`ports` is preferred", async () => {
const port = await getPort({ ports: [8080] });
expect(port).toEqual(8080);
});
});

describe("localhost", () => {
test("default port is not in use", async () => {
const port = await getPort({ host: "localhost" });
Expand Down Expand Up @@ -117,7 +124,7 @@ describe("errors", () => {
random: false,
}).catch((error) => error);
expect(error.toString()).toMatchInlineSnapshot(
`"GetPortError: Unable to find an available port on host "192.168.1.999" (tried 3000, 3000-3100)"`,
`"GetPortError: Unable to find an available port on host "192.168.1.999" (tried 3000-3100)"`,
);
});
});
Expand Down