Skip to content

v14 no longer supports bodies for 204 responses #3000

Description

@alxndrsn

Please avoid duplicates

Reproducible test case

to be included in issue body

Nock Version

13, 14

Node Version

24

TypeScript Version

No response

What happened?

This may be reasonable behaviour, but I thought worth calling out in case anyone else is surprised when upgrading from nock v13 to v14.

It looks like in v13, it was possible to include a response body with a 204 ("No Content") response, but in v14 the body is now ignored.

I couldn't find any reference to this change in Issues.

Repro

demo.js

const assert = require('node:assert');
const http = require('node:http');

const nock = require('nock');

nock('http://api.example.com')
    .get('/')
    .reply(204, {});

const req = http.request('http://api.example.com/', res => {
  let body = '';
  res.on('data', data => body += data);
  res.on('end', () => {
    assert.equal(res.statusCode, 204);
    assert.equal(body, '{}');
  });
});
req.end();

nock v13

$ rm yarn.lock; echo '{ "name":"nock-demo" }' >package.json && echo 'nodeLinker: node-modules' >.yarnrc.yml && volta pin node@24 && volta pin yarn@4 && yarn && yarn add nock@13 && node demo.js; echo "demo.js exited with status: $?"
success: pinned node@24.19.0 (with npm@11.17.0) in package.json
success: pinned yarn@4.18.0 in package.json
➤ YN0000: · Yarn 4.18.0
➤ YN0000: ┌ Resolution step
➤ YN0000: └ Completed
➤ YN0000: ┌ Fetch step
➤ YN0000: └ Completed
➤ YN0000: ┌ Link step
➤ YN0000: └ Completed
➤ YN0000: · Done in 0s 49ms
➤ YN0000: · Yarn 4.18.0
➤ YN0000: ┌ Resolution step
➤ YN0085: │ + nock@npm:13.5.6, debug@npm:4.4.3, json-stringify-safe@npm:5.0.1, ms@npm:2.1.3, propagate@npm:2.0.1
➤ YN0000: └ Completed in 0s 504ms
➤ YN0000: ┌ Fetch step
➤ YN0000: └ Completed
➤ YN0000: ┌ Link step
➤ YN0000: └ Completed
➤ YN0000: · Done in 0s 550ms
demo.js exited with status: 0

nock v14

$ rm yarn.lock; echo '{ "name":"nock-demo" }' >package.json && echo 'nodeLinker: node-modules' >.yarnrc.yml && volta pin node@24 && volta pin yarn@4 && yarn && yarn add nock@14 && node demo.js; echo "demo.js exited with status: $?"
success: pinned node@24.19.0 (with npm@11.17.0) in package.json
success: pinned yarn@4.18.0 in package.json
➤ YN0000: · Yarn 4.18.0
➤ YN0000: ┌ Resolution step
➤ YN0000: └ Completed
➤ YN0000: ┌ Fetch step
➤ YN0000: └ Completed
➤ YN0000: ┌ Link step
➤ YN0000: └ Completed
➤ YN0000: · Done in 0s 48ms
➤ YN0000: · Yarn 4.18.0
➤ YN0000: ┌ Resolution step
➤ YN0085: │ + nock@npm:14.0.17, @mswjs/interceptors@npm:0.41.9, @open-draft/deferred-promise@npm:2.2.0, @open-draft/logger@npm:0.3.0, and 6 more.
➤ YN0000: └ Completed in 1s 815ms
➤ YN0000: ┌ Fetch step
➤ YN0000: └ Completed
➤ YN0000: ┌ Link step
➤ YN0000: └ Completed
➤ YN0000: · Done in 1s 910ms
node:assert:152
  throw new AssertionError(obj);
  ^

AssertionError [ERR_ASSERTION]: '' == '{}'
    at IncomingMessage.<anonymous> (/home/user/temp/nock-204/demo.js:15:12)
    at IncomingMessage.emit (node:events:521:24)
    at endReadableNT (node:internal/streams/readable:1736:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:90:21) {
  generatedMessage: true,
  code: 'ERR_ASSERTION',
  actual: '',
  expected: '{}',
  operator: '==',
  diff: 'simple'
}

Node.js v24.19.0
demo.js exited with status: 1

Would you be interested in contributing a fix?

  • yes

Expected

Maybe helpful to either:

  1. restore previous behaviour, or
  2. throw if a body is supplied when calling .reply(204, ...)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions