Skip to content

fix: avoid starting backend server during tests#258

Open
pggdev wants to merge 1 commit into
volcano-sh:mainfrom
pggdev:fix/server-starting-during-test
Open

fix: avoid starting backend server during tests#258
pggdev wants to merge 1 commit into
volcano-sh:mainfrom
pggdev:fix/server-starting-during-test

Conversation

@pggdev

@pggdev pggdev commented May 16, 2026

Copy link
Copy Markdown

Description

#257

Updated the backend startup logic so importing app from server.js does not start the HTTP server.

Changes

  • Moved app.listen(...) into an exported startServer function
  • Added a direct-run guard so the server starts only when server.js is executed directly
  • Kept app importable for Supertest without opening port 3001

Testing

  • Ran backend tests with npm run test -- --run

Signed-off-by: pggdev <princegupta.ns153@gmail.com>
@volcano-sh-bot

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign william-wang for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the server startup logic in backend/src/server.js by encapsulating it within an exported startServer function and implementing a main module guard for direct execution. The review feedback suggests adopting the node: prefix for built-in module imports and using realpathSync to ensure the main module check is robust against symbolic links.

Comment thread backend/src/server.js
@@ -1,5 +1,6 @@
import express from "express";
import cors from "cors";
import { pathToFileURL } from "url";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

In ECMAScript Modules (ESM), it is a best practice to use the node: prefix when importing built-in Node.js modules (e.g., node:url) to clearly distinguish them from npm packages. Additionally, I've included realpathSync from node:fs to support a more robust main module check in the direct-run guard.

import { pathToFileURL } from "node:url";
import { realpathSync } from "node:fs";

Comment thread backend/src/server.js
Comment on lines +1015 to +1020
if (
process.argv[1] &&
import.meta.url === pathToFileURL(process.argv[1]).href
) {
startServer();
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The current check for the main module using process.argv[1] can be brittle. import.meta.url always contains the fully resolved path (with symbolic links resolved), whereas process.argv[1] might contain a symbolic link path depending on how the script was invoked. Using realpathSync ensures both paths are fully resolved before comparison, making the guard more robust across different deployment and execution environments.

Suggested change
if (
process.argv[1] &&
import.meta.url === pathToFileURL(process.argv[1]).href
) {
startServer();
}
if (
process.argv[1] &&
import.meta.url === pathToFileURL(realpathSync(process.argv[1])).href
) {
startServer();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants