Skip to content

[BUG] Elysia server provider test suite entirely broken — stale mocks after Node.js HTTP refactor #1204

@CrepuscularIRIS

Description

@CrepuscularIRIS

Bug Description

All 6 tests in elysia-server-provider.spec.ts fail because the test mocks target the old app.listen() API, but startServer() was refactored to use http.createServer() + server.listen(). The mocked Elysia app.listen is never called, and the real Node.js HTTP server tries to bind port 3000, hitting EADDRINUSE.

Location

packages/server-elysia/src/elysia-server-provider.spec.ts
packages/server-elysia/src/elysia-server-provider.ts:43-92

Reproduction

npx vitest run packages/server-elysia/src/elysia-server-provider.spec.ts --reporter=verbose

All 6 tests fail:

FAIL  should start the server → EADDRINUSE 0.0.0.0:3000
FAIL  should stop the server → EADDRINUSE 0.0.0.0:3000
FAIL  should throw if already running → EADDRINUSE 0.0.0.0:3000
FAIL  should configure websocket if enabled → EADDRINUSE 0.0.0.0:3000
FAIL  should extract and display custom endpoints from configureApp → EADDRINUSE 0.0.0.0:3000
FAIL  should handle startup errors and release port → expected "Startup failed" but got "EADDRINUSE"

Stack Trace

Error: listen EADDRINUSE: address already in use 0.0.0.0:3000
Serialized Error: { code: 'EADDRINUSE', errno: -98, syscall: 'listen', address: '0.0.0.0', port: 3000 }

Root Cause

The startServer() method (lines 43-92) was refactored from using Elysia's app.listen() to creating a Node.js http.createServer() with a request adapter. The implementation comment at line 42 explains: "Elysia's app.listen() is designed for Bun runtime and doesn't work properly in Node.js."

However, the test file still mocks mockApp.listen (spec line 25) and expects it to be called (spec line 58). Since startServer now calls createServer from node:http instead of app.listen(), the mock is never hit, and the real HTTP server tries to bind port 3000.

Impact

The Elysia server provider has zero test coverage. Any regression in the Elysia adapter would go undetected.

Suggested Fix

The tests need to mock node:http.createServer instead of app.listen:

import { createServer } from "node:http";

vi.mock("node:http", () => ({
  createServer: vi.fn().mockReturnValue({
    listen: vi.fn((port, host, cb) => cb()),
    close: vi.fn((cb) => cb()),
    once: vi.fn(),
    listening: false,
  }),
}));

Found via automated testing and codebase analysis. Happy to submit a PR if confirmed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions