Skip to content

Bug: The "listener" argument must be of type Function. Received type object #1239

@caub

Description

@caub

What is the expected behavior?
It should work!

What is the actual behavior?
It crashes

Possible solution
no idea, yet

How to reproduce the issue/test case

const http = require('http');
const https = require('https');

const nock = require('nock');
nock('https://httpbin.org').persist()
  .post('/post').reply(200, (uri, reqBody) => {
    return {
      "hits": {
        "total": 0
      }
    }
  });

const fetch = (url, o) => new Promise((resolve, reject) => {
  let body = o && o.body;
  if (typeof body === 'object') {
    o.headers = { ...o.headers, 'content-type': o.headers && o.headers['content-type'] || 'application/json' };
    body = JSON.stringify(body);
  }
  const req = (/^https:/.test(o && o.protocol || url) ? https : http).request(url, o);
  req.once('error', reject);
  req.once('response', res => {
    const bufs = [];
    res.on('data', buf => bufs.push(buf));
    res.on('end', () => {
      const text = Buffer.concat(bufs);
      if (res.headers.location) return fetch(res.headers.location, o || url).then(resolve)
      resolve(/^application\/json/.test(res.headers['content-type']) ? JSON.parse(text) : text);
    });
    res.on('error', reject);
  });
  if (body && typeof body.pipe === 'function') return body.pipe(req);
  if (body) {
    req.write(body);
  }
  req.end();
});

(async () => {
  await fetch('https://httpbin.org/post', { method: 'POST', body: { wat: 1 } }).then(console.log, console.error);
})()

Versions

Software Version(s)
Nock 10.0.0
Node 10.11.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions