Skip to content

Commit 8e9250d

Browse files
authored
fix: remove use of node-fetch, use built in fetch (#714)
1 parent 9926e22 commit 8e9250d

File tree

9 files changed

+30
-27
lines changed

9 files changed

+30
-27
lines changed

package.json

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,9 @@
2929
"@types/jest": "^29.5.2",
3030
"chalk": "^4.1.2",
3131
"inquirer": "^8.2.3",
32-
"node-fetch": "^2.x",
3332
"ora": "^5.4.1",
3433
"semver": "^7.5.2"
3534
},
36-
"overrides": {
37-
"node-fetch@^2.x": {
38-
"whatwg-url": "14.x"
39-
}
40-
},
4135
"devDependencies": {
4236
"@adobe/eslint-config-aio-lib-config": "^4.0.0",
4337
"acorn": "^8.8.2",
@@ -52,7 +46,6 @@
5246
"eslint-plugin-promise": "^6.6.0",
5347
"execa": "^5.1.1",
5448
"jest": "^29.0.1",
55-
"jest-fetch-mock": "^3.0.0",
5649
"jest-junit": "^16.0.0",
5750
"jest-plugin-fs": "^2.9.0",
5851
"oclif": "^4",

src/commands/discover.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
*/
1212

1313
const { Command, Flags, ux } = require('@oclif/core')
14-
const fetch = require('node-fetch')
1514
const inquirer = require('inquirer')
1615
const { sortValues } = require('../helpers')
1716

src/helpers.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
const fetch = require('node-fetch')
1413
const fs = require('fs')
1514
const inquirer = require('inquirer')
1615

test/.eslintrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"globals": {
3+
"setFetchMock": true
4+
},
25
"rules": {
36
"node/no-unpublished-require": 0
47
}

test/commands/discover.test.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
const fetch = require('node-fetch')
1413
const inquirer = require('inquirer')
1514
const TheCommand = require('../../src/commands/discover')
1615
const { stdout } = require('stdout-stderr')
@@ -20,7 +19,6 @@ jest.mock('inquirer')
2019
let command
2120

2221
beforeEach(() => {
23-
fetch.resetMocks()
2422
command = new TheCommand([])
2523
command.config = {
2624
commands: [{ pluginName: '@adobe/aio-cli-plugin-baz' }],
@@ -44,13 +42,14 @@ describe('sorting', () => {
4442
]
4543
}
4644
beforeEach(() => {
47-
fetch.mockResponseOnce(JSON.stringify(expectedResult))
45+
setFetchMock(true, expectedResult)
4846
})
4947

5048
test('unknown sort-field', async () => {
51-
fetch.mockResponseOnce(JSON.stringify({
49+
setFetchMock(true, {
5250
objects: []
53-
}))
51+
})
52+
5453
command.argv = ['--sort-field', 'unknown']
5554
await expect(command.run()).rejects.toThrow('Expected --sort-field=')
5655
})
@@ -100,7 +99,7 @@ test('interactive install', async () => {
10099
{ package: { name: '@adobe/aio-cli-plugin-baz', description: 'some baz', version: '1.0.2', date: dayAfter } }
101100
]
102101
}
103-
fetch.mockResponseOnce(JSON.stringify(expectedResult))
102+
setFetchMock(true, expectedResult)
104103

105104
command.argv = ['-i']
106105
inquirer.prompt = jest.fn().mockResolvedValue({
@@ -121,7 +120,7 @@ test('interactive install - no choices', async () => {
121120
{ package: { name: '@adobe/aio-cli-plugin-baz', description: 'some baz', version: '1.0.2', date: now } }
122121
]
123122
}
124-
fetch.mockResponseOnce(JSON.stringify(expectedResult))
123+
setFetchMock(true, expectedResult)
125124

126125
command.argv = ['-i']
127126
inquirer.prompt = jest.fn().mockResolvedValue({
@@ -132,7 +131,9 @@ test('interactive install - no choices', async () => {
132131
})
133132

134133
test('json result error', async () => {
135-
fetch.mockResponse()
134+
const errorMessage = 'Invalid JSON response'
135+
setFetchMock(false, errorMessage)
136+
136137
command.argv = []
137-
await expect(command.run()).rejects.toThrow('FetchError: invalid json response body')
138+
await expect(command.run()).rejects.toThrow(errorMessage)
138139
})

test/commands/rollback.test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
const fetch = require('node-fetch')
1413
const inquirer = require('inquirer')
1514
const { stdout } = require('stdout-stderr')
1615
const helpers = require('../../src/helpers')
@@ -30,7 +29,6 @@ let command
3029

3130
beforeEach(() => {
3231
jest.clearAllMocks()
33-
fetch.resetMocks()
3432
command = new TheCommand([])
3533
command.config = {
3634
commands: [{ pluginName: 'baz' }],

test/commands/update.test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
const fetch = require('node-fetch')
1413
const inquirer = require('inquirer')
1514
const helpers = require('../../src/helpers')
1615
const { stdout } = require('stdout-stderr')
@@ -30,7 +29,6 @@ let command
3029

3130
beforeEach(() => {
3231
jest.clearAllMocks()
33-
fetch.resetMocks()
3432
command = new TheCommand([])
3533
command.config = {
3634
commands: [],

test/helpers.test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
const fetch = require('node-fetch')
1413
const { prompt, sortValues, getNpmLatestVersion, getNpmLocalVersion, hideNPMWarnings } = require('../src/helpers')
1514
const inquirer = require('inquirer')
1615
const { stderr } = require('stdout-stderr')
@@ -20,7 +19,6 @@ jest.mock('fs')
2019
jest.mock('inquirer')
2120

2221
beforeEach(() => {
23-
fetch.resetMocks()
2422
fs.readFileSync.mockRestore()
2523
})
2624

@@ -205,7 +203,7 @@ test('getNpmLatestVersion', async () => {
205203
}
206204
}
207205

208-
fetch.mockResponseOnce(JSON.stringify(json))
206+
setFetchMock(true, json)
209207
return expect(getNpmLatestVersion('foo')).resolves.toEqual(json['dist-tags'].latest)
210208
})
211209

test/jest.setup.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,22 @@ const { stdout } = require('stdout-stderr')
1515
jest.setTimeout(3000)
1616
jest.useFakeTimers()
1717

18-
const fetch = require('jest-fetch-mock')
19-
jest.setMock('node-fetch', fetch)
18+
const originalFetch = global.fetch
19+
20+
afterEach(() => {
21+
global.fetch = originalFetch
22+
})
23+
24+
beforeEach(() => {
25+
global.fetch = jest.fn()
26+
})
27+
28+
global.setFetchMock = (ok = true, mockData = {}) => {
29+
global.fetch = jest.fn().mockResolvedValue({
30+
ok,
31+
json: () => ok ? Promise.resolve(mockData) : Promise.reject(mockData)
32+
})
33+
}
2034

2135
// trap console log
2236
// note: if you use console.log, some of these tests will start failing because they depend on the order/position of the output

0 commit comments

Comments
 (0)