actions / actions/toolkit

Unit testing: Bad credentials error

Open
#1,115 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
TypeScript
Stars
5.9k
Forks
1.8k
PR merge metrics
No merged PRs in 30d

Description

I'm using nock to mock HTTP requests following the readme - mocking-the-octokit-client

While my test suite completes successfully, I get the following error message:

::error::Bad credentials
node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
          ^

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "HttpError: Bad credentials".] {
  code: 'ERR_UNHANDLED_REJECTION'

image

Is there something else I need to configure or is it a bug?

// index.js
import * as core from '@actions/core';
import * as github from '@actions/github';

export default async function run() {
    try {
        const myToken = core.getInput('repo_token');
        const octokit = github.getOctokit({ auth: myToken});
        const { data: repos } = await octokit.rest.repos.listPublic();
        return repos;
    }
    catch (error) {
        core.setFailed(error.message);
        throw error;
    }
}

run();
// index.test.js
import nock from 'nock';
import action from './index.js';
import { listPublicRepositories } from './fixtures/list-public-repositories';

describe('action test suite', () => {
  it('It returns a list of public repositories', async () => {

    const repoToken = 'token';
    process.env['INPUT_REPO_TOKEN'] = repoToken;

    nock('https://api.github.com')
      .persist()
      .get('/repositories')
      .reply(200, listPublicRepositories);
    
      const repos = await action();

      expect(repos.length).toBe(1);
      expect(repos[0].id).toBe(1296269);
      expect(repos[0].node_id).toBe("MDEwOlJlcG9zaXRvcnkxMjk2MjY5");
  });
});

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with index.js and index.test.js, then compare the setup with the linked mocking-the-octokit-client README section. Run the unit test and trace the request and promise handling around action(); done means the mocked test completes without the Bad credentials error or an unhandled promise rejection.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.