Skip to content

Requests not being intercepted on Node 18 #2336

@erunion

Description

@erunion

Please avoid duplicates

Reproducible test case

RunKit does not support Node 18 yet.

Nock Version

13.2.4

Node Version

v18.0.0

TypeScript Version

No response

What happened?

Nock is currently unable to intercept requests on Node 18 with its native fetch implementation:

const nock = require('nock');

const mock = nock('https://httpbin.org').get('/anything').reply(200, 'request was mocked');

fetch('https://httpbin.org/anything')
  .then(res => res.text())
  .then(res => {
    console.log(res)
    mock.done();
  });
(node:5294) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "br, gzip, deflate", 
    "Accept-Language": "*", 
    "Host": "httpbin.org", 
    "Sec-Fetch-Mode": "cors", 
    "User-Agent": "undici", 
    "X-Amzn-Trace-Id": "REDACTED"
  }, 
  "json": null, 
  "method": "GET", 
  "origin": "REDACTED", 
  "url": "https://httpbin.org/anything"
}

node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^

AssertionError [ERR_ASSERTION]: Mocks not yet satisfied:
GET https://httpbin.org:443/anything
    at Scope.done (/Users/erunion/debug/node_modules/nock/lib/scope.js:166:12)
    at /Users/erunion/code/debug/index.js:9:10
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  generatedMessage: false,
  code: 'ERR_ASSERTION',
  actual: false,
  expected: true,
  operator: '=='
}

Node.js v18.0.0

The same code on Node 16 and 14 with the fetch polyfill isomorphic-fetch:

require('isomorphic-fetch');
const nock = require('nock');

const mock = nock('https://httpbin.org').get('/anything').reply(200, 'request was mocked');

fetch('https://httpbin.org/anything')
  .then(res => res.text())
  .then(res => {
    console.log(res)
    mock.done();
  });
request was mocked

Would you be interested in contributing a fix?

  • yes

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