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

Test getClient #509

Merged
merged 1 commit into from
Jul 10, 2023
Merged

Test getClient #509

merged 1 commit into from
Jul 10, 2023

Conversation

chadwhitacre
Copy link
Member

@chadwhitacre chadwhitacre commented Jul 7, 2023

Part of #482, before #503 (came up on #503).

We've been putting off testing getClient. No longer!

The strategy here is to mock at a lower level: OctokitWithReplies.

@codecov
Copy link

codecov bot commented Jul 8, 2023

Codecov Report

❗ No coverage uploaded for pull request base (main@6c1342f). Click here to learn what that means.
Patch coverage: 25.00% of modified lines in pull request are covered.

❗ Current head bc80527 differs from pull request most recent head 74b497f. Consider uploading reports for the commit 74b497f to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #509   +/-   ##
=======================================
  Coverage        ?   84.47%           
=======================================
  Files           ?       97           
  Lines           ?     2538           
  Branches        ?      496           
=======================================
  Hits            ?     2144           
  Misses          ?      387           
  Partials        ?        7           
Impacted Files Coverage Δ
src/api/github/octokitWithRetries.ts 0.00% <0.00%> (ø)
src/api/github/getClient.ts 83.87% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@@ -1,23 +1,16 @@
# direnv will make a copy of this file as an .env file
# Follow the instructions in README.md as how to add your personal
# tokens and secrets in this file
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need all this boilerplate in the test env, it's copy-pasta from the example env.


# GitHub App ID
# GitHub
FORCE_USER_TOKEN_GITHUB_CLIENT="false"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bringing this up here as it belongs with GitHub config.

# NOTE: this *must* be on a single line, otherwise it will break
# reading the secret key.
GH_APP_SECRET_KEY='-----BEGIN RSA PRIVATE KEY----------END RSA PRIVATE KEY-----'
GH_APP_SECRET_KEY="top \nsecret\n key"
Copy link
Member Author

@chadwhitacre chadwhitacre Jul 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switching to a more test-friendly value, also exercising the \n-stripping behavior.

Comment on lines 26 to 33
OctokitWithRetries.apps = {
getOrgInstallation: jest.fn(async ({ org }) => {
return {
data: {
id: `installation-${org}`
}
};
}),
};
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is new, it's needed now since we call this inside getClient.

};

OctokitWithRetries.orgs = {
checkMembershipForUser: jest.fn(async function ({ org, username }) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took an opportunity to clean up the call signature here a bit, I had been using (x) ... x.org previously. Besides the apps addition above, this is the only other material change to the mock client API, sorry I don't have a clean diff for you.


const _CLIENTS_BY_ORG = new Map();
const OctokitWithRetries = Octokit.plugin(retry);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Factoring out for mockability.

GETSENTRY_REPO: 'getsentry',
SENTRY_REPO: 'sentry',
};
});
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This duplicates the .env.test config and the .env.test config takes precendence, so let's consolidate everything there.

@@ -1,16 +1,4 @@
/* eslint-env jest */
jest.mock('@api/slack');
jest.mock('@api/github/getClient');
jest.mock('@api/github/octokitWithRetries');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💃

@chadwhitacre chadwhitacre marked this pull request as ready for review July 8, 2023 10:53
@chadwhitacre chadwhitacre force-pushed the cwlw/test-getclient branch from bc80527 to 74b497f Compare July 8, 2023 12:16
@chadwhitacre
Copy link
Member Author

Rebased for linting.


import { MockOctokitError } from './mockError';

// Looks goofy, don't it? This is what I could figure out to mock a class. I
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What were you aiming for here? I think a manual mock here seems fine as the implementation details are clear and can be used across files

@chadwhitacre chadwhitacre merged commit 7e1154c into main Jul 10, 2023
@chadwhitacre chadwhitacre deleted the cwlw/test-getclient branch July 10, 2023 17:54
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

Successfully merging this pull request may close these issues.

2 participants