Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

[AI Dev Tools Zoomcamp] Node.js test runner hangs when testing Socket.io and Express integration

Aperta Adatta ai principianti
#397 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 3 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
85/100
Tipo di issue
Documentazione
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
express, javascript, node.js

Direzione di ricerca

Inizia esaminando le modifiche proposte nell’issue per server/index.js, tests/integration.test.js e package.json. Verifica che la guida documentata includa l’avvio protetto del server, la pulizia di Socket.io e del server HTTP e il comando di test; il lavoro è completato quando le FAQ spiegano chiaramente come evitare il blocco o l’errore ERR_SERVER_ALREADY_LISTEN.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Course & Homework
  • Course: AI Dev Tools Zoomcamp (2026)
  • Module: Homework 2 - Build and Ship an AI-Assisted Full-Stack App
Problem Description

When running integration tests for a collaborative full-stack application using Node.js's built-in test runner (node --test) against an Express and Socket.io server, the test process hangs indefinitely in the terminal after executing, or fails with ERR_SERVER_ALREADY_LISTEN.

Root Cause
  1. Premature port binding: If server.listen(PORT) is executed at the top level of server/index.js, importing the app/server into tests/integration.test.js causes the HTTP server to bind before the test hook can bind to an ephemeral port (0).
  2. Open handles: Socket.io and http.Server maintain persistent event loop connections and internal heartbeat timers, preventing Node's test runner from detecting an empty event loop.
Solution & Best Practice
  1. Guard server.listen in server/index.js:
    Ensure the server only listens when executed directly, not when imported by test suites:

    if (require.main === module) {
      server.listen(PORT, () => {
        console.log(`Server listening on port ${PORT}`);
      });
    }
    
  2. Explicitly close both io and server in the test cleanup hook:

    after(async () => {
      io.close();
      await new Promise((resolve) => serverInstance.close(resolve));
    });
    
  3. Use --test-force-exit in package.json:
    Add --test-force-exit so Node exits cleanly once all tests pass:

    "scripts": {
      "test": "node --test --test-force-exit tests/*.test.js"
    }
    
Lingua principale
Python
Stelle
7
Fork
19
Merge medio
3g 20h
PR unite (30g)
9

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di DataTalksClub/faq

Tutte le issue di DataTalksClub/faq

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.