nodejs / nodejs/undici

Using the MockAgent and the pipeline method

Open
#2,791 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
JavaScript
Stars
7.7k
Forks
880
Avg merge
2d 16h
Merged PRs (30d)
68

Description

Bug Description

Pipeline mock fails when node:stream/promise pipeline is used.

Reproducible By

The first test passes. For the second test, the await pipeline(...) fails. The error is not catched.

// Import Node.js Dependencies
import { describe, it, afterEach } from "node:test";
import { pipeline } from "node:stream/promises";
import stream, { PassThrough, Readable } from "node:stream";
import assert from "node:assert";
import timers from "node:timers/promises";

// Import Third-Party Dependencies
import undici, { MockAgent } from "undici";

describe("undici.pipeline", async() => {
  let kMockAgent: MockAgent;

  afterEach(async() => {
    await kMockAgent.close();
  });

  it("OK : stream.pipeline", async() => {
    kMockAgent = new MockAgent();
    kMockAgent.disableNetConnect();
    undici.setGlobalDispatcher(kMockAgent);

    kMockAgent
      .get("https://boz.com")
      .intercept({ method: "POST", path: "/" })
      .reply(200, "Bar");

    const readable = Readable.from("Some text.".repeat(10_000));
    const request = undici.pipeline(
      "https://boz.com",
      { method: "POST" },
      ({ body }) => body
    );

    stream.pipeline(readable, request, new PassThrough(), () => void 0);
    await timers.setTimeout(100);

    assert.ok(1);
  });

  it("KO : stream/promises.pipeline", async() => {
    kMockAgent = new MockAgent();
    kMockAgent.disableNetConnect();
    undici.setGlobalDispatcher(kMockAgent);

    kMockAgent
      .get("https://boz.com")
      .intercept({ method: "POST", path: "/" })
      .reply(200, "Bar");

    const readable = Readable.from("Some text.".repeat(10_000));
    const request = undici.pipeline(
      "https://boz.com",
      { method: "POST" },
      ({ body }) => body
    );

    try {
      await pipeline(readable, request, new PassThrough());
    }
    catch (error) {
      console.error(error);
    }

    assert.ok(1);
  });
});

Expected Behavior

stream/promises.pipeline should work in the same way as stream/pipeline ?

Logs & Screenshots

image

Environment

Node 20.x
Undici 6.6.2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided Node 20 reproduction and compare undici.pipeline with node:stream/pipeline and node:stream/promises.pipeline. Inspect the pipeline and MockAgent behavior involved in the example, then add a regression test covering the promise-based pipeline. Done means the mocked request completes without an uncaught error and both pipeline forms behave consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.