From 9c9f8a266f2316434daa266bf75396b5e7d1e058 Mon Sep 17 00:00:00 2001 From: "James M. Greene" Date: Thu, 28 Mar 2024 14:55:36 -0500 Subject: [PATCH] Update tests to use the Octokit RequestError class --- package-lock.json | 1 + package.json | 1 + src/api-client.test.js | 39 +++++++++++++++++++++++++++------------ 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7592e30..c98859f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "espree": "^9.6.1" }, "devDependencies": { + "@octokit/request-error": "^5.0.1", "@vercel/ncc": "^0.38.1", "eslint": "^8.57.0", "eslint-config-prettier": "^8.8.0", diff --git a/package.json b/package.json index 5554788..068872a 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "espree": "^9.6.1" }, "devDependencies": { + "@octokit/request-error": "^5.0.1", "@vercel/ncc": "^0.38.1", "eslint": "^8.57.0", "eslint-config-prettier": "^8.8.0", diff --git a/src/api-client.test.js b/src/api-client.test.js index 1238497..513f218 100644 --- a/src/api-client.test.js +++ b/src/api-client.test.js @@ -1,9 +1,24 @@ const core = require('@actions/core') const apiClient = require('./api-client') +const { RequestError } = require('@octokit/request-error') const mockGetPages = jest.fn() const mockCreatePagesSite = jest.fn() +const generateRequestError = statusCode => { + const fakeRequest = { headers: {}, url: '/' } + const fakeResponse = { status: statusCode } + let message = 'Oops' + if (statusCode === 404) { + message = 'Not Found' + } + if (statusCode === 409) { + message = 'Too Busy' + } + const error = new RequestError(message, statusCode, { request: fakeRequest, response: fakeResponse }) + return error +} + jest.mock('@actions/github', () => ({ context: { repo: { @@ -48,7 +63,7 @@ describe('apiClient', () => { }) it('handles a 409 response when the page already exists', async () => { - mockCreatePagesSite.mockImplementationOnce(() => Promise.reject({ response: { status: 409 } })) + mockCreatePagesSite.mockImplementationOnce(() => Promise.reject(generateRequestError(409))) // Simply assert that no error is raised const result = await apiClient.enablePagesSite({ @@ -59,7 +74,7 @@ describe('apiClient', () => { }) it('re-raises errors on failure status codes', async () => { - mockCreatePagesSite.mockImplementationOnce(() => Promise.reject({ response: { status: 404 } })) + mockCreatePagesSite.mockImplementationOnce(() => Promise.reject(generateRequestError(404))) let erred = false try { @@ -86,7 +101,7 @@ describe('apiClient', () => { }) it('re-raises errors on failure status codes', async () => { - mockGetPages.mockImplementationOnce(() => Promise.reject({ response: { status: 404 } })) + mockGetPages.mockImplementationOnce(() => Promise.reject(generateRequestError(404))) let erred = false try { @@ -105,7 +120,7 @@ describe('apiClient', () => { it('does not make a request to create a page if it already exists', async () => { const PAGE_OBJECT = { html_url: 'https://actions.github.io/is-awesome/' } mockGetPages.mockImplementationOnce(() => Promise.resolve({ status: 200, data: PAGE_OBJECT })) - mockCreatePagesSite.mockImplementationOnce(() => Promise.reject({ response: { status: 404 } })) + mockCreatePagesSite.mockImplementationOnce(() => Promise.reject(generateRequestError(404))) const result = await apiClient.findOrCreatePagesSite({ githubToken: GITHUB_TOKEN @@ -117,7 +132,7 @@ describe('apiClient', () => { it('makes request to create a page by default if it does not exist', async () => { const PAGE_OBJECT = { html_url: 'https://actions.github.io/is-awesome/' } - mockGetPages.mockImplementationOnce(() => Promise.reject({ response: { status: 404 } })) + mockGetPages.mockImplementationOnce(() => Promise.reject(generateRequestError(404))) mockCreatePagesSite.mockImplementationOnce(() => Promise.resolve({ status: 201, data: PAGE_OBJECT })) const result = await apiClient.findOrCreatePagesSite({ @@ -130,7 +145,7 @@ describe('apiClient', () => { it('makes a request to create a page when explicitly enabled if it does not exist', async () => { const PAGE_OBJECT = { html_url: 'https://actions.github.io/is-awesome/' } - mockGetPages.mockImplementationOnce(() => Promise.reject({ response: { status: 404 } })) + mockGetPages.mockImplementationOnce(() => Promise.reject(generateRequestError(404))) mockCreatePagesSite.mockImplementationOnce(() => Promise.resolve({ status: 201, data: PAGE_OBJECT })) const result = await apiClient.findOrCreatePagesSite({ @@ -143,8 +158,8 @@ describe('apiClient', () => { }) it('does not make a request to create a page when explicitly disabled even if it does not exist', async () => { - mockGetPages.mockImplementationOnce(() => Promise.reject({ response: { status: 404 } })) - mockCreatePagesSite.mockImplementationOnce(() => Promise.reject({ response: { status: 500 } })) // just so they both aren't 404 + mockGetPages.mockImplementationOnce(() => Promise.reject(generateRequestError(404))) + mockCreatePagesSite.mockImplementationOnce(() => Promise.reject(generateRequestError(500))) // just so they both aren't 404 let erred = false try { @@ -163,8 +178,8 @@ describe('apiClient', () => { }) it('does not make a second request to get page if create fails for reason other than existence', async () => { - mockGetPages.mockImplementationOnce(() => Promise.reject({ response: { status: 404 } })) - mockCreatePagesSite.mockImplementationOnce(() => Promise.reject({ response: { status: 500 } })) // just so they both aren't 404 + mockGetPages.mockImplementationOnce(() => Promise.reject(generateRequestError(404))) + mockCreatePagesSite.mockImplementationOnce(() => Promise.reject(generateRequestError(500))) // just so they both aren't 404 let erred = false try { @@ -184,9 +199,9 @@ describe('apiClient', () => { it('makes second request to get page if create fails because of existence', async () => { const PAGE_OBJECT = { html_url: 'https://actions.github.io/is-awesome/' } mockGetPages - .mockImplementationOnce(() => Promise.reject({ response: { status: 404 } })) + .mockImplementationOnce(() => Promise.reject(generateRequestError(404))) .mockImplementationOnce(() => Promise.resolve({ status: 200, data: PAGE_OBJECT })) - mockCreatePagesSite.mockImplementationOnce(() => Promise.reject({ response: { status: 409 } })) + mockCreatePagesSite.mockImplementationOnce(() => Promise.reject(generateRequestError(409))) const result = await apiClient.findOrCreatePagesSite({ githubToken: GITHUB_TOKEN