nock / nock

HTTP server mocking and expectations library for Node.js
MIT License
12.7k stars 736 forks source link

replyWithError with object does not work since beta-8+ #2789

Open mikicho opened 3 days ago

mikicho commented 3 days ago

Please avoid duplicates

Reproducible test case

it('allows json response', done => {

Nock Version

14.beta-8+

Node Version

20

TypeScript Version

it('allows json response', done => { const scope = nock('http://example.test') .post('/echo') .replyWithError({ message: 'Service not found', code: 'test' })

const req = http.request({
  host: 'example.test',
  method: 'POST',
  path: '/echo',
  port: 80,
})

req.on('error', e => {
  expect(e).to.deep.equal({
    message: 'Service not found',
    code: 'test',
  })
  scope.done()
  done()
})

req.end()

})

What happened?

Copy of this to discuss about a workaround and follow the error from nock POV

Would you be interested in contributing a fix?

mikicho commented 3 days ago

@marikaner If it happens for interceptors with the replyWithError function, you can make sure you throw an Error object.