Skip to content

[BUG]: can not intercept requests with nock #551

@mac2000

Description

@mac2000

What happened?

Suddenly all tests depending on nock starts to fail

mkdir foo
cd foo
npm init -y -f
npm i -S @octokit/rest nock

demo.js

const { Octokit } = require('@octokit/rest')
const nock = require('nock')
const fetch = require('node-fetch')

nock.recorder.rec()
nock("https://api.github.com")
      .post("/graphql")
      .reply(200, {data: {hello: "world"}});

const octokit = new Octokit({request:{fetch}})
octokit.graphql(`{ whatever }`)
    .then(res => console.log(res))
    .catch(err => console.log(err.message)) // This endpoint requires you to be authenticated

Instead of expected, mocked response I'm receiving and error "This endpoint requires you to be authenticated"

Which makes me believe tests like this 1323-parameter-deprecation-bug-test.js will also fail

Meanwhile, node complains:

(node:41108) 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)

When enabling trace warning as node suggests I see this piece of code in @octokit/request.js:

const fetch = requestOptions.request && requestOptions.request.fetch || globalThis.fetch || /* istanbul ignore next */nodeFetch;

and indeed if I will put nodeFetch in first place, suddenly everything starts to work

current workaround is already suggested somewhere in stackoverflow

so we may want to do something like this (note that request.js library depends on node-fetch):

const fetch = require('node-fetch')
const octokit = new Octokit({request:{fetch}})

Versions

"@octokit/rest": "^19.0.7"
"nock": "^13.3.0"

node: v18.5.0

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type: SupportAny questions, information, or general needs around the SDK or GitHub APIs

    Type

    No type

    Projects

    Status

    ✅ Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions