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(jest-dev-server): fix port detection #518

Merged
merged 1 commit into from
Feb 4, 2023
Merged
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
49 changes: 27 additions & 22 deletions packages/jest-dev-server/src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* eslint-disable no-console */
import stream from "stream";
import net from "net";
import { Transform } from "node:stream";
import { createServer } from "node:http";
import { promisify } from "node:util";
import chalk from "chalk";
import spawnd from "spawnd";
import cwd from "cwd";
import waitOn from "wait-on";
import findProcess from "find-process";
import { promisify } from "util";
import treeKill from "tree-kill";
import prompts from "prompts";

Expand All @@ -23,7 +23,7 @@ const DEFAULT_CONFIG = {

const pTreeKill = promisify(treeKill);

const serverLogPrefixer = new stream.Transform({
const serverLogPrefixer = new Transform({
transform(chunk, encoding, callback) {
this.push(chalk.magentaBright(`[Jest Dev server] ${chunk.toString()}`));
callback();
Expand Down Expand Up @@ -91,22 +91,27 @@ async function outOfStin(block) {
return result;
}

async function getIsPortTaken(config) {
let server;
const cleanupAndReturn = (result) =>
new Promise((resolve) => {
server.once("close", () => resolve(result)).close();
});
const val = await new Promise((resolve, reject) => {
server = net
.createServer()
.once("error", (err) =>
err.code === "EADDRINUSE" ? resolve(cleanupAndReturn(true)) : reject()
)
.once("listening", () => resolve(cleanupAndReturn(false)))
.listen(config.port, config.host);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the comment on this PR which has already been merged! But I think removing config.host from here, caused the issue: #555.

I've submitted a new PR addressing this 😁 #556

/**
* Check if a port is busy.
* @param {*} config
*/
async function checkIsPortBusy(config) {
return new Promise((resolve) => {
const server = createServer()
.once("error", (err) => {
console.log("error");
if (err.code === "EADDRINUSE") {
resolve(true);
} else {
resolve(false);
}
})
.once("listening", () => {
console.log("listening");
server.once("close", () => resolve(false)).close();
})
.listen(config.port);
});
return val;
}

export async function setup(providedConfigs) {
Expand Down Expand Up @@ -172,12 +177,12 @@ async function setupJestServer(providedConfig, index) {
}

if (config.port) {
const isPortTaken = await getIsPortTaken(config);
if (isPortTaken) {
const isPortBusy = await checkIsPortBusy(config);
if (isPortBusy) {
await usedPortHandler();
}

if (config.usedPortAction === "ignore" && isPortTaken) {
if (config.usedPortAction === "ignore" && isPortBusy) {
console.log("");
console.log("Port is already taken. Assuming server is already running.");
} else {
Expand Down