nodejs / nodejs/node

Test runner with isolation=none does not work in child process

Aperta
#60,020 6 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

child_process test_runner
Lingua principale
JavaScript
Stelle
122k
Fork
37.3k
Merge medio
4g 2h
PR unite (30g)
283

Descrizione

Version

v24.9.0

Platform
Linux home 5.15.0-153-generic #163-Ubuntu SMP Thu Aug 7 16:37:18 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
Subsystem

No response

What steps will reproduce the bug?

runner.js

import cluster from 'node:cluster';
import { run } from "node:test";

if (cluster.isPrimary) {
  cluster.fork();
} else {
  const stream = run({
    isolation: "none",
  });

  stream.on("data", (data) => {
    console.log("on data", data.type);
  });

  stream.on("end", () => {
    console.log("on end")
    process.exit(0);
  });

  stream.on("error", (err) => {
    console.log("on error", err);
  });
}

dummy.test.js

import { describe, it } from "node:test";
import * as assert from "node:assert";

describe("suite", () => {
  it("works", ()  => {
    assert.ok(true);
  });
});

How often does it reproduce? Is there a required condition?

Always. No.

What is the expected behavior? Why is that the expected behavior?

With isolation: "process" (the default), here's the output:

on data test:enqueue
on data test:dequeue
on data test:enqueue
on data test:dequeue
on data test:enqueue
on data test:dequeue
on data test:complete
on data test:start
on data test:start
on data test:pass
on data test:complete
on data test:plan
on data test:pass
on data test:summary
on data test:complete
on data test:plan
on data test:diagnostic
on data test:diagnostic
on data test:diagnostic
on data test:diagnostic
on data test:diagnostic
on data test:diagnostic
on data test:diagnostic
on data test:diagnostic
on data test:summary
on end

Process finished with exit code 0

The process cleanly exits at the end of the tests.

What do you see instead?

With isolation: "none", here's the output:

on data test:enqueue
on data test:dequeue
on data test:enqueue
on data test:dequeue
on data test:complete
on data test:start
on data test:start
on data test:pass
on data test:complete
on data test:plan
on data test:pass

And the process hangs.

Additional information

The goal was to use the "cluster" module to run the tests in parallel with the shard option:

import { run } from "node:test";
import { spec } from "node:test/reporters";
import { PassThrough } from "node:stream";
import { availableParallelism } from "node:os";
import cluster from 'node:cluster';

const concurrency = availableParallelism();
let completed = 0;

if (cluster.isPrimary) {
  const mergedStream = new PassThrough({
    objectMode: true
  });

  cluster.on("message", (_worker, message) => {
    mergedStream.emit("data", message);
  });

  mergedStream
    .compose(spec)
    .pipe(process.stdout);

  for (let i = 1; i <= concurrency; i++) {
    cluster.fork({
      SHARD_INDEX: i,
      SHARD_TOTAL: concurrency,
    });
  }

  cluster.on("exit", (worker, code, signal) => {
    if (++completed === concurrency) {
      mergedStream.end();
    }
  });
} else {
  const stream = run({
    isolation: "none",
    shard: {
      index: parseInt(process.env.SHARD_INDEX, 10),
      total: parseInt(process.env.SHARD_TOTAL, 10),
    },
  });

  stream.on("data", (chunk) => {
    process.send(chunk);
  });
}

Related: https://github.com/nodejs/node/issues/55939

Guida per i contributori

Apri la guida per i contributori

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.

Direzione di ricerca

Inizia con la riproduzione in runner.js e dummy.test.js, quindi traccia il punto di ingresso run() di node:test con l'isolamento impostato su "none" quando viene invocato da un processo figlio di cluster. Confronta il suo ciclo di vita dello stream con l'isolamento predefinito "process" e l'issue correlata 55939. È completato quando lo stream del processo figlio emette gli eventi rimanenti, termina ed esce correttamente, mantenendo il supporto per gli shard.

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

Valutazione

Stack tecnologico
javascript, node.js
Ambito
testing-qa
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Attiva
Chiarezza
Abbastanza chiara
Idoneità per principianti
55/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.