diff --git a/package.json b/package.json index e2e3a9d..70c62bd 100644 --- a/package.json +++ b/package.json @@ -29,15 +29,9 @@ "@types/jest": "^29.5.2", "chalk": "^4.1.2", "inquirer": "^8.2.3", - "node-fetch": "^2.x", "ora": "^5.4.1", "semver": "^7.5.2" }, - "overrides": { - "node-fetch@^2.x": { - "whatwg-url": "14.x" - } - }, "devDependencies": { "@adobe/eslint-config-aio-lib-config": "^4.0.0", "acorn": "^8.8.2", @@ -52,7 +46,6 @@ "eslint-plugin-promise": "^6.6.0", "execa": "^5.1.1", "jest": "^29.0.1", - "jest-fetch-mock": "^3.0.0", "jest-junit": "^16.0.0", "jest-plugin-fs": "^2.9.0", "oclif": "^4", diff --git a/src/commands/discover.js b/src/commands/discover.js index a4e29a6..8db3db6 100644 --- a/src/commands/discover.js +++ b/src/commands/discover.js @@ -11,7 +11,6 @@ */ const { Command, Flags, ux } = require('@oclif/core') -const fetch = require('node-fetch') const inquirer = require('inquirer') const { sortValues } = require('../helpers') diff --git a/src/helpers.js b/src/helpers.js index 820f7f2..dfb9c18 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -10,7 +10,6 @@ * governing permissions and limitations under the License. */ -const fetch = require('node-fetch') const fs = require('fs') const inquirer = require('inquirer') diff --git a/test/.eslintrc.json b/test/.eslintrc.json index 9738bc9..2b006ab 100644 --- a/test/.eslintrc.json +++ b/test/.eslintrc.json @@ -1,4 +1,7 @@ { + "globals": { + "setFetchMock": true + }, "rules": { "node/no-unpublished-require": 0 } diff --git a/test/commands/discover.test.js b/test/commands/discover.test.js index a0ecc18..1fece0e 100644 --- a/test/commands/discover.test.js +++ b/test/commands/discover.test.js @@ -10,7 +10,6 @@ * governing permissions and limitations under the License. */ -const fetch = require('node-fetch') const inquirer = require('inquirer') const TheCommand = require('../../src/commands/discover') const { stdout } = require('stdout-stderr') @@ -20,7 +19,6 @@ jest.mock('inquirer') let command beforeEach(() => { - fetch.resetMocks() command = new TheCommand([]) command.config = { commands: [{ pluginName: '@adobe/aio-cli-plugin-baz' }], @@ -44,13 +42,14 @@ describe('sorting', () => { ] } beforeEach(() => { - fetch.mockResponseOnce(JSON.stringify(expectedResult)) + setFetchMock(true, expectedResult) }) test('unknown sort-field', async () => { - fetch.mockResponseOnce(JSON.stringify({ + setFetchMock(true, { objects: [] - })) + }) + command.argv = ['--sort-field', 'unknown'] await expect(command.run()).rejects.toThrow('Expected --sort-field=') }) @@ -100,7 +99,7 @@ test('interactive install', async () => { { package: { name: '@adobe/aio-cli-plugin-baz', description: 'some baz', version: '1.0.2', date: dayAfter } } ] } - fetch.mockResponseOnce(JSON.stringify(expectedResult)) + setFetchMock(true, expectedResult) command.argv = ['-i'] inquirer.prompt = jest.fn().mockResolvedValue({ @@ -121,7 +120,7 @@ test('interactive install - no choices', async () => { { package: { name: '@adobe/aio-cli-plugin-baz', description: 'some baz', version: '1.0.2', date: now } } ] } - fetch.mockResponseOnce(JSON.stringify(expectedResult)) + setFetchMock(true, expectedResult) command.argv = ['-i'] inquirer.prompt = jest.fn().mockResolvedValue({ @@ -132,7 +131,9 @@ test('interactive install - no choices', async () => { }) test('json result error', async () => { - fetch.mockResponse() + const errorMessage = 'Invalid JSON response' + setFetchMock(false, errorMessage) + command.argv = [] - await expect(command.run()).rejects.toThrow('FetchError: invalid json response body') + await expect(command.run()).rejects.toThrow(errorMessage) }) diff --git a/test/commands/rollback.test.js b/test/commands/rollback.test.js index 1a7dfd4..354a843 100644 --- a/test/commands/rollback.test.js +++ b/test/commands/rollback.test.js @@ -10,7 +10,6 @@ * governing permissions and limitations under the License. */ -const fetch = require('node-fetch') const inquirer = require('inquirer') const { stdout } = require('stdout-stderr') const helpers = require('../../src/helpers') @@ -30,7 +29,6 @@ let command beforeEach(() => { jest.clearAllMocks() - fetch.resetMocks() command = new TheCommand([]) command.config = { commands: [{ pluginName: 'baz' }], diff --git a/test/commands/update.test.js b/test/commands/update.test.js index 47ae0e6..6dd8759 100644 --- a/test/commands/update.test.js +++ b/test/commands/update.test.js @@ -10,7 +10,6 @@ * governing permissions and limitations under the License. */ -const fetch = require('node-fetch') const inquirer = require('inquirer') const helpers = require('../../src/helpers') const { stdout } = require('stdout-stderr') @@ -30,7 +29,6 @@ let command beforeEach(() => { jest.clearAllMocks() - fetch.resetMocks() command = new TheCommand([]) command.config = { commands: [], diff --git a/test/helpers.test.js b/test/helpers.test.js index 49b9a76..698aca9 100644 --- a/test/helpers.test.js +++ b/test/helpers.test.js @@ -10,7 +10,6 @@ * governing permissions and limitations under the License. */ -const fetch = require('node-fetch') const { prompt, sortValues, getNpmLatestVersion, getNpmLocalVersion, hideNPMWarnings } = require('../src/helpers') const inquirer = require('inquirer') const { stderr } = require('stdout-stderr') @@ -20,7 +19,6 @@ jest.mock('fs') jest.mock('inquirer') beforeEach(() => { - fetch.resetMocks() fs.readFileSync.mockRestore() }) @@ -205,7 +203,7 @@ test('getNpmLatestVersion', async () => { } } - fetch.mockResponseOnce(JSON.stringify(json)) + setFetchMock(true, json) return expect(getNpmLatestVersion('foo')).resolves.toEqual(json['dist-tags'].latest) }) diff --git a/test/jest.setup.js b/test/jest.setup.js index 5868b7c..c3cd0cd 100644 --- a/test/jest.setup.js +++ b/test/jest.setup.js @@ -15,8 +15,22 @@ const { stdout } = require('stdout-stderr') jest.setTimeout(3000) jest.useFakeTimers() -const fetch = require('jest-fetch-mock') -jest.setMock('node-fetch', fetch) +const originalFetch = global.fetch + +afterEach(() => { + global.fetch = originalFetch +}) + +beforeEach(() => { + global.fetch = jest.fn() +}) + +global.setFetchMock = (ok = true, mockData = {}) => { + global.fetch = jest.fn().mockResolvedValue({ + ok, + json: () => ok ? Promise.resolve(mockData) : Promise.reject(mockData) + }) +} // trap console log // note: if you use console.log, some of these tests will start failing because they depend on the order/position of the output