Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 task done
mac2000 opened this issue Feb 24, 2023 · 2 comments
Closed
1 task done

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

mac2000 opened this issue Feb 24, 2023 · 2 comments
Labels
Type: Support Any questions, information, or general needs around the SDK or GitHub APIs

Comments

@mac2000
Copy link

mac2000 commented Feb 24, 2023

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
@mac2000 mac2000 added Status: Triage This is being looked at and prioritized Type: Bug Something isn't working as documented labels Feb 24, 2023
@gr2m
Copy link
Contributor

gr2m commented Feb 24, 2023

The latest Node comes with a built-in fetch which cannot be intercepted by nock, I think.

I would recommend to mock with fetch-mock instead, like this:

const mock = fetchMock.sandbox().getOnce(
"https://api.github.com/",
{ ok: true },
{
headers: {
accept: "application/vnd.github.v3+json",
"user-agent": userAgent,
},
}
);
const octokit = new Octokit({
request: {
fetch: mock,

@gr2m gr2m added Type: Support Any questions, information, or general needs around the SDK or GitHub APIs and removed Type: Bug Something isn't working as documented labels Feb 24, 2023
@gr2m gr2m removed the Status: Triage This is being looked at and prioritized label Feb 24, 2023
@nickfloyd nickfloyd moved this from 🆕 Triage to 🛑 Blocked/Awaiting Response in 🧰 Octokit Active Mar 10, 2023
@kfcampbell
Copy link
Member

Closing as the question has been answered. Please open additional issue(s) for additional questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Support Any questions, information, or general needs around the SDK or GitHub APIs
Projects
Archived in project
Development

No branches or pull requests

3 participants