diff --git a/functions/classes/ExtensionHandler.js b/functions/classes/ExtensionHandler.js index bf058600..e5bdee2b 100644 --- a/functions/classes/ExtensionHandler.js +++ b/functions/classes/ExtensionHandler.js @@ -3,8 +3,8 @@ import parse from 'diffparser' import { Handler } from './Handler' export class ExtensionHandler extends Handler { - constructor(githubToken, namespace, fetch = null) { - super(githubToken, fetch) + constructor(githubToken, namespace) { + super(githubToken) this.namespace = namespace } diff --git a/functions/classes/Handler.js b/functions/classes/Handler.js index 94752c5a..5d8f2d06 100644 --- a/functions/classes/Handler.js +++ b/functions/classes/Handler.js @@ -1,17 +1,11 @@ import { Octokit } from '@octokit/rest' export class Handler { - constructor(githubToken, fetch = null) { + constructor(githubToken) { const options = { auth: githubToken, } - if (fetch) { - options.request = { - fetch, - } - } - this.githubClient = new Octokit(options) } diff --git a/package.json b/package.json index 0a779f7d..09ca1e8c 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "@babel/core": "^7.25.9", "@babel/eslint-parser": "^7.25.9", "@babel/preset-env": "^7.25.9", + "@fetch-mock/jest": "^0.2.0", "babel-loader": "^9.2.1", "babel-plugin-source-map-support": "^2.2.0", "eslint": "^8.57.1", @@ -22,7 +23,6 @@ "eslint-plugin-jsx-a11y": "^6.10.1", "eslint-plugin-prettier": "^5.2.1", "eslint-plugin-react": "^7.37.2", - "fetch-mock": "^11.1.5", "jest": "^29.7.0", "nock": "^13.5.4", "node-fetch": "2", diff --git a/tests/extension.spec.js b/tests/extension.spec.js index 21e854f2..462660f6 100644 --- a/tests/extension.spec.js +++ b/tests/extension.spec.js @@ -1,5 +1,5 @@ import nock from 'nock' -import fetchMock from 'fetch-mock' +import fetchMock from '@fetch-mock/jest' import { ExtensionHandler } from '../functions/classes/ExtensionHandler' import { handler } from '../functions/extension' @@ -124,12 +124,7 @@ describe('Validating extension', () => { }) test('file extension is not txt', async () => { - const mock = fetchMock - .sandbox() - .mock( - 'https://api.github.com/repos/foo/bar/statuses/ee55a1223ce20c3e7cb776349cb7f8efb7b88511', - 200 - ) + fetchMock.mockGlobal().route('*', 200) nock('http://git.hub').get('/diff').replyWithFile(200, `${__dirname}/fixtures/no_txt.diff`) @@ -151,7 +146,7 @@ describe('Validating extension', () => { }, } - const extension = new ExtensionHandler('GH_TOKEN', 'Test config', mock) + const extension = new ExtensionHandler('GH_TOKEN', 'Test config') await extension.handle(githubEvent, callback) expect(callback).toHaveBeenCalledTimes(1) @@ -160,21 +155,20 @@ describe('Validating extension', () => { statusCode: 204, }) - const lastOptions = JSON.parse(mock.lastOptions().body) - expect(lastOptions).toStrictEqual({ - context: 'Test config - File extension check', - description: 'Fail: "theatlantic.com" has not a txt extension', - state: 'failure', - }) + expect(fetch).toHaveFetched( + 'https://api.github.com/repos/foo/bar/statuses/ee55a1223ce20c3e7cb776349cb7f8efb7b88511', + { + body: { + context: 'Test config - File extension check', + description: 'Fail: "theatlantic.com" has not a txt extension', + state: 'failure', + }, + } + ) }) test('at least one file extension is not txt', async () => { - const mock = fetchMock - .sandbox() - .mock( - 'https://api.github.com/repos/foo/bar/statuses/ee55a1223ce20c3e7cb776349cb7f8efb7b88511', - 200 - ) + fetchMock.mockGlobal().route('*', 200) nock('http://git.hub') .get('/diff') @@ -198,7 +192,7 @@ describe('Validating extension', () => { }, } - const extension = new ExtensionHandler('GH_TOKEN', 'Test config', mock) + const extension = new ExtensionHandler('GH_TOKEN', 'Test config') await extension.handle(githubEvent, callback) expect(callback).toHaveBeenCalledTimes(1) @@ -207,21 +201,20 @@ describe('Validating extension', () => { statusCode: 204, }) - const lastOptions = JSON.parse(mock.lastOptions().body) - expect(lastOptions).toStrictEqual({ - context: 'Test config - File extension check', - description: 'Fail: "wow.gamona.de" has not a txt extension', - state: 'failure', - }) + expect(fetch).toHaveFetched( + 'https://api.github.com/repos/foo/bar/statuses/ee55a1223ce20c3e7cb776349cb7f8efb7b88511', + { + body: { + context: 'Test config - File extension check', + description: 'Fail: "wow.gamona.de" has not a txt extension', + state: 'failure', + }, + } + ) }) test('one deleted file', async () => { - const mock = fetchMock - .sandbox() - .mock( - 'https://api.github.com/repos/foo/bar/statuses/ee55a1223ce20c3e7cb776349cb7f8efb7b88511', - 200 - ) + fetchMock.mockGlobal().route('*', 200) nock('http://git.hub') .get('/diff') @@ -245,7 +238,7 @@ describe('Validating extension', () => { }, } - const extension = new ExtensionHandler('GH_TOKEN', 'Test config', mock) + const extension = new ExtensionHandler('GH_TOKEN', 'Test config') await extension.handle(githubEvent, callback) expect(callback).toHaveBeenCalledTimes(1) @@ -254,21 +247,20 @@ describe('Validating extension', () => { statusCode: 204, }) - const lastOptions = JSON.parse(mock.lastOptions().body) - expect(lastOptions).toStrictEqual({ - context: 'Test config - File extension check', - description: 'passed', - state: 'success', - }) + expect(fetch).toHaveFetched( + 'https://api.github.com/repos/foo/bar/statuses/ee55a1223ce20c3e7cb776349cb7f8efb7b88511', + { + body: { + context: 'Test config - File extension check', + description: 'passed', + state: 'success', + }, + } + ) }) test('file extension is ok', async () => { - const mock = fetchMock - .sandbox() - .mock( - 'https://api.github.com/repos/foo/bar/statuses/ee55a1223ce20c3e7cb776349cb7f8efb7b88511', - 200 - ) + fetchMock.mockGlobal().route('*', 200) nock('http://git.hub').get('/diff').replyWithFile(200, `${__dirname}/fixtures/with_a.txt.diff`) @@ -290,7 +282,7 @@ describe('Validating extension', () => { }, } - const extension = new ExtensionHandler('GH_TOKEN', 'Test config', mock) + const extension = new ExtensionHandler('GH_TOKEN', 'Test config') await extension.handle(githubEvent, callback) expect(callback).toHaveBeenCalledTimes(1) @@ -299,11 +291,15 @@ describe('Validating extension', () => { statusCode: 204, }) - const lastOptions = JSON.parse(mock.lastOptions().body) - expect(lastOptions).toStrictEqual({ - context: 'Test config - File extension check', - description: 'passed', - state: 'success', - }) + expect(fetch).toHaveFetched( + 'https://api.github.com/repos/foo/bar/statuses/ee55a1223ce20c3e7cb776349cb7f8efb7b88511', + { + body: { + context: 'Test config - File extension check', + description: 'passed', + state: 'success', + }, + } + ) }) }) diff --git a/tests/weblate.spec.js b/tests/weblate.spec.js index 4ff762a6..86bbda14 100644 --- a/tests/weblate.spec.js +++ b/tests/weblate.spec.js @@ -1,4 +1,4 @@ -import fetchMock from 'fetch-mock' +import fetchMock from '@fetch-mock/jest' import { WeblateHandler } from '../functions/classes/WeblateHandler' import { handler } from '../functions/weblate' @@ -123,9 +123,7 @@ describe('Apply label', () => { }) test('PR is ok', async () => { - const mock = fetchMock - .sandbox() - .mock('https://api.github.com/repos/foo/bar/issues/42/labels', 200) + fetchMock.mockGlobal().route('*', 200) const callback = jest.fn() const githubEvent = { @@ -147,7 +145,7 @@ describe('Apply label', () => { }, } - const weblate = new WeblateHandler('GH_TOKEN', mock) + const weblate = new WeblateHandler('GH_TOKEN') await weblate.handle(githubEvent, callback) expect(callback).toHaveBeenCalledTimes(1) @@ -156,6 +154,8 @@ describe('Apply label', () => { statusCode: 204, }) - expect(mock.lastOptions().body).toBe('{"labels":["Translations"]}') + expect(fetch).toHaveFetched('https://api.github.com/repos/foo/bar/issues/42/labels', { + body: { labels: ['Translations'] }, + }) }) }) diff --git a/yarn.lock b/yarn.lock index 6c2d552c..beb84bc4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1445,6 +1445,13 @@ resolved "https://registry.yarnpkg.com/@eslint/js/-/js-8.57.1.tgz#de633db3ec2ef6a3c89e2f19038063e8a122e2c2" integrity sha512-d9zaMRSTIKDLhctzH12MtXvJKSSUhaHcjV+2Z+GK+EEY7XKpP5yR4x+N3TAcHTcu963nIr+TMcCb4DBCYX1z6Q== +"@fetch-mock/jest@^0.2.0": + version "0.2.0" + resolved "https://registry.yarnpkg.com/@fetch-mock/jest/-/jest-0.2.0.tgz#70fae50fd7886c313b0186c9d18165a80e080109" + integrity sha512-4oKbWKklbk7inBuK2YRtdjiJFG47N0VzkzTI9TOzY0g3q92605bwtcJXnAZ13gr4C0L0O/Bt24EIt0+aNDSd1Q== + dependencies: + fetch-mock "12.0.0" + "@hapi/accept@^6.0.1": version "6.0.3" resolved "https://registry.yarnpkg.com/@hapi/accept/-/accept-6.0.3.tgz#eef0800a4f89cd969da8e5d0311dc877c37279ab" @@ -4307,15 +4314,15 @@ fb-watchman@^2.0.0: dependencies: bser "2.1.1" -fetch-mock@^11.1.5: - version "11.1.5" - resolved "https://registry.yarnpkg.com/fetch-mock/-/fetch-mock-11.1.5.tgz#77f78942f3733cfba47fc232b8528d1138a6761f" - integrity sha512-KHmZDnZ1ry0pCTrX4YG5DtThHi0MH+GNI9caESnzX/nMJBrvppUHMvLx47M0WY9oAtKOMiPfZDRpxhlHg89BOA== +fetch-mock@12.0.0: + version "12.0.0" + resolved "https://registry.yarnpkg.com/fetch-mock/-/fetch-mock-12.0.0.tgz#d531e0c58aa3ecc04fa225fc4b624d070563dbc8" + integrity sha512-JSsjzoRN4rYqHa2/+8ushJGDsK9HGNTdBZo6Hrpu3KFN7Y03nRCt2VJ2WG4OUvyTUukOQ4TQIfjcFcEkMPGZ0Q== dependencies: "@types/glob-to-regexp" "^0.4.4" dequal "^2.0.3" glob-to-regexp "^0.4.1" - is-subset "^0.1.1" + is-subset-of "^3.1.10" regexparam "^3.0.0" file-entry-cache@^6.0.1: @@ -4893,10 +4900,12 @@ is-string@^1.0.5, is-string@^1.0.7: dependencies: has-tostringtag "^1.0.0" -is-subset@^0.1.1: - version "0.1.1" - resolved "https://registry.yarnpkg.com/is-subset/-/is-subset-0.1.1.tgz#8a59117d932de1de00f245fcdd39ce43f1e939a6" - integrity sha512-6Ybun0IkarhmEqxXCNw/C0bna6Zb/TkfUX9UbwJtK6ObwAVCxmAP308WWTHviM/zAqXk05cdhYsUsZeGQh99iw== +is-subset-of@^3.1.10: + version "3.1.10" + resolved "https://registry.yarnpkg.com/is-subset-of/-/is-subset-of-3.1.10.tgz#d3f4331b9ca288318fae92ad5d953241b6f7b00b" + integrity sha512-avvaYgVmYWyaZ1NDFiv4y9JGkrE2je3op1Po4VYKKJKR8H2qVPsg1GZuuXl5elCTxTlwAIsrAjWAs4BVrISFRw== + dependencies: + typedescriptor "3.0.2" is-symbol@^1.0.2, is-symbol@^1.0.3: version "1.0.4" @@ -6996,6 +7005,11 @@ typed-array-length@^1.0.6: is-typed-array "^1.1.13" possible-typed-array-names "^1.0.0" +typedescriptor@3.0.2: + version "3.0.2" + resolved "https://registry.yarnpkg.com/typedescriptor/-/typedescriptor-3.0.2.tgz#9ad1715bc2be1cf063d5acbc4cd4bfc96d644225" + integrity sha512-hyVbaCUd18UiXk656g/imaBLMogpdijIEpnhWYrSda9rhvO4gOU16n2nh7xG5lv/rjumnZzGOdz0CEGTmFe0fQ== + unbox-primitive@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/unbox-primitive/-/unbox-primitive-1.0.2.tgz#29032021057d5e6cdbd08c5129c226dff8ed6f9e"