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

Testing Using Nock does not work #1977

Open
allout58 opened this issue Feb 19, 2024 · 6 comments
Open

Testing Using Nock does not work #1977

allout58 opened this issue Feb 19, 2024 · 6 comments

Comments

@allout58
Copy link

Bug Report

It looks like in v8 of @octokit/requests, they switched from using Node's HTTP api, to using fetch. The latest published nock does not have support for fetch, so it does not catch the requests.

Current Behavior
As described in documentation and set up by create-probot-app, using nock does not intercept requests.

describe('My App', () => {
  test('app', () => {
    const mock = nock('https://api.github.com')
            // Test that we correctly return a test token
            .post('/app/installations/2/access_tokens')
            .reply(200, {
                token: 'test',
                permissions: {
                    metadata: 'read',
                },
            });
        await probot.receive({name: 'repository', payload});

        expect(mock.pendingMocks()).toStrictEqual([]);
  });
});

API requests get through and fail because of invalid keys, tokens, or installation IDs

Expected behavior/code

Documented examples of testing should work and not let requests through.

Environment

  • Probot version(s): 13.0.2
  • Node/npm version: Node 18.12.1, NPM 8.18.2
  • OS: Windows 11
  • Nock: 13

Possible Solution

  1. Update documentation to warn that nock does not currently work. Update create-probot-app to include nock@beta
    -- or --
  2. Somehow use the options.request.fetch option with a custom mock function in @octokit/request
Copy link

welcome bot commented Feb 19, 2024

Thanks for opening this issue. A contributor should be by to give feedback soon. In the meantime, please check out the contributing guidelines and explore other ways you can get involved.

@Uzlopak
Copy link
Collaborator

Uzlopak commented Feb 19, 2024

use nock 14 beta, which should work with native fetch.

@allout58
Copy link
Author

I figured that out while writing this bug, but it would be nice to have it documented somewhere. Maybe I should have put this in the doc's repo, but since it also applies to the create-probot-app I thought it made more sense to put it here

@Uzlopak
Copy link
Collaborator

Uzlopak commented Feb 19, 2024

PRs are welcome. But somehow it feels wrong to document it now. If nock 14 becomes stable I would say yes.

@wolfy1339
Copy link
Collaborator

create-probot-app isn't up to date with the new changes from v13 yet.

@wolfy1339
Copy link
Collaborator

You can use node-fetch yourself if you'd like, and avoid using the built in fetch API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants