From 08746025c3e6e483f92167cbfb381730f6310659 Mon Sep 17 00:00:00 2001 From: Shazron Abdullah Date: Wed, 22 Jan 2025 00:37:48 +0800 Subject: [PATCH] fix: remove node-fetch, use built-in fetch --- package.json | 2 -- src/telemetry-lib.js | 1 - test/hooks.test.js | 3 +-- test/index.test.js | 4 +--- test/jest.setup.js | 8 ++++++-- test/telemetry-lib.test.js | 27 ++++++++++++++++++++++++--- 6 files changed, 32 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index bcbe458..e56c474 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,6 @@ "ci-info": "^4.0.0", "debug": "^4.1.1", "inquirer": "^8.2.1", - "node-fetch": "^2.6.7", "os-name": "^4.0.1", "splunk-logging": "^0.11.1" }, @@ -28,7 +27,6 @@ "eslint-plugin-standard": "^4.0.1", "execa": "^4.0.2", "jest": "^29", - "jest-fetch-mock": "^3.0.0", "jest-junit": "^13.0.0", "memfs": "^4.6.0", "oclif": "^4.3.6", diff --git a/src/telemetry-lib.js b/src/telemetry-lib.js index e17c1b5..1d23fee 100644 --- a/src/telemetry-lib.js +++ b/src/telemetry-lib.js @@ -9,7 +9,6 @@ OF ANY KIND, either express or implied. See the License for the specific languag governing permissions and limitations under the License. */ -const fetch = require('node-fetch') const config = require('@adobe/aio-lib-core-config') const osName = require('os-name') const inquirer = require('inquirer') diff --git a/test/hooks.test.js b/test/hooks.test.js index 8ab83f8..dbb7dba 100644 --- a/test/hooks.test.js +++ b/test/hooks.test.js @@ -10,7 +10,6 @@ * governing permissions and limitations under the License. */ -const fetch = require('node-fetch') const inquirer = require('inquirer') const config = require('@adobe/aio-lib-core-config') @@ -28,7 +27,7 @@ const mockPackageJson = { describe('hook interfaces', () => { beforeEach(() => { - fetch.mockReset() + global.setFetchMock() }) test('command-error', async () => { diff --git a/test/index.test.js b/test/index.test.js index 2f14f73..60f8138 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -10,7 +10,6 @@ * governing permissions and limitations under the License. */ -const fetch = require('node-fetch') const TheCommand = require('../src/commands/telemetry') const { stdout } = require('stdout-stderr') @@ -19,9 +18,8 @@ jest.mock('inquirer') let command beforeEach(() => { - fetch.resetMocks() command = new TheCommand([]) - fetch.mockResponseOnce('ok') + global.setFetchMock() }) test('exports a run function', async () => { diff --git a/test/jest.setup.js b/test/jest.setup.js index ce4de83..d10f962 100644 --- a/test/jest.setup.js +++ b/test/jest.setup.js @@ -16,8 +16,12 @@ const { vol } = require('memfs') jest.setTimeout(3000) jest.useFakeTimers() -const fetch = require('jest-fetch-mock') -jest.setMock('node-fetch', fetch) +global.setFetchMock = (ok = true, mockData = {}) => { + global.fetch = jest.fn().mockResolvedValue({ + ok, + json: () => ok ? Promise.resolve(mockData) : Promise.reject(mockData) + }) +} vol.reset() diff --git a/test/telemetry-lib.test.js b/test/telemetry-lib.test.js index a249c5b..b84c852 100644 --- a/test/telemetry-lib.test.js +++ b/test/telemetry-lib.test.js @@ -10,16 +10,19 @@ * governing permissions and limitations under the License. */ -const fetch = require('node-fetch') const telemetryLib = require('../src/telemetry-lib') const config = require('@adobe/aio-lib-core-config') jest.mock('@adobe/aio-lib-core-config') - +jest.mock('debug', () => { + global.mockDebug = jest.fn() + return () => global.mockDebug +}) describe('telemetry-lib', () => { beforeEach(() => { jest.resetModules() - fetch.mockReset() + global.mockDebug.mockReset() + global.setFetchMock() }) test('exports messages', async () => { @@ -43,10 +46,28 @@ describe('telemetry-lib', () => { test('uses client id from config', async () => { config.get.mockReturnValue('clientidxyz') telemetryLib.init('a@4', 'binTest2') + await telemetryLib.trackEvent('test-event') + + expect(config.get).toHaveBeenCalledWith('binTest2-cli-telemetry.clientId') + expect(config.get).toHaveBeenCalledWith('binTest2-cli-telemetry.optOut', 'global') + expect(fetch).toHaveBeenCalledWith(expect.any(String), + expect.objectContaining({ body: expect.stringContaining('"clientId":"clientidxyz"') })) + expect(global.mockDebug).not.toHaveBeenCalledWith(expect.stringContaining('error reaching telemetry server')) + }) + + test('fetch error', async () => { + const error = 'some error' + global.fetch = jest.fn().mockRejectedValue(error) + + config.get.mockReturnValue('clientidxyz') + telemetryLib.init('a@4', 'binTest2') + await telemetryLib.trackEvent('test-event') + expect(config.get).toHaveBeenCalledWith('binTest2-cli-telemetry.clientId') expect(config.get).toHaveBeenCalledWith('binTest2-cli-telemetry.optOut', 'global') expect(fetch).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({ body: expect.stringContaining('"clientId":"clientidxyz"') })) + expect(global.mockDebug).toHaveBeenCalledWith(expect.stringContaining('error reaching telemetry server'), error) }) })