Skip to content

Commit c3c3100

Browse files
shazronpurplecabbage
authored andcommitted
fix: remove node-fetch, use built-in fetch (#843)
* fix: remove node-fetch, use built-in fetch --------- Co-authored-by: Jesse MacFadyen <purplecabbage@gmail.com>
1 parent 7ae8722 commit c3c3100

8 files changed

Lines changed: 17 additions & 36 deletions

File tree

.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"fixtureJson": true,
99
"fixtureHjson": true,
1010
"fixtureYaml": true,
11-
"fakeFileSystem": true
11+
"fakeFileSystem": true,
12+
"setFetchMock": true
1213
},
1314
"settings": {
1415
"jsdoc": {

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
"junk": "^3.1.0",
3939
"lodash.clonedeep": "^4.5.0",
4040
"node-abort-controller": "^3.1.1",
41-
"node-fetch": "^2.6.7",
4241
"open": "^8.4.2",
4342
"ora": "^5",
4443
"pure-http": "^3",

src/lib/audit-logger.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA
88
OF ANY KIND, either express or implied. See the License for the specific language
99
governing permissions and limitations under the License.
1010
*/
11-
const fetch = require('node-fetch')
1211
const fs = require('fs')
1312
const path = require('path')
1413
const chalk = require('chalk')

test/__fixtures__/pack/2.all.config.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,6 @@
379379
"dependencies": {
380380
"@adobe/generator-app-excshell": "^0.2.4",
381381
"@adobe/aio-sdk": "^3.0.0",
382-
"node-fetch": "^2.6.0",
383382
"core-js": "^3.6.4",
384383
"react": "^16.13.1",
385384
"react-dom": "^16.13.1",

test/__fixtures__/pack/4.all.config.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@
167167
"version": "0.0.1",
168168
"private": true,
169169
"dependencies": {
170-
"@adobe/aio-sdk": "^3.0.0",
171-
"node-fetch": "^2.6.0"
170+
"@adobe/aio-sdk": "^3.0.0"
172171
},
173172
"devDependencies": {
174173
"jest": "^27.2.4",

test/__fixtures__/pack/6.all.config.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@
167167
"version": "0.0.1-invalidversion",
168168
"private": true,
169169
"dependencies": {
170-
"@adobe/aio-sdk": "^3.0.0",
171-
"node-fetch": "^2.6.0"
170+
"@adobe/aio-sdk": "^3.0.0"
172171
},
173172
"devDependencies": {
174173
"jest": "^27.2.4",

test/commands/lib/audit-logger.test.js

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,7 @@ governing permissions and limitations under the License.
1111
*/
1212

1313
const fs = require('fs')
14-
/* eslint-disable no-unused-vars */
15-
const path = require('path')
16-
/* eslint-disable no-unused-vars */
17-
const chalk = require('chalk')
18-
const fetch = require('node-fetch')
19-
jest.mock('node-fetch', () => jest.fn())
2014
const auditLogger = require('../../../src/lib/audit-logger')
21-
const { getCliEnv } = require('@adobe/aio-lib-env')
2215

2316
jest.mock('fs')
2417
jest.mock('chalk', () => ({
@@ -41,28 +34,12 @@ const mockLogEvent = {
4134
orgId: 'mockorg'
4235
}
4336

44-
const mockResponse = Promise.resolve({
45-
ok: true,
46-
status: 200,
47-
text: () => {
48-
return {}
49-
}
50-
})
51-
52-
const mockErrorResponse = Promise.resolve({
53-
ok: false,
54-
status: 400,
55-
text: () => {
56-
return {}
57-
}
58-
})
59-
6037
beforeEach(() => {
61-
fetch.mockReset()
38+
setFetchMock(true, 200, {})
6239
})
6340

6441
test('sendAuditLogs with valid params', async () => {
65-
fetch.mockReturnValue(mockResponse)
42+
setFetchMock(true, 200, {})
6643
const options = {
6744
method: 'POST',
6845
headers: {
@@ -78,7 +55,7 @@ test('sendAuditLogs with valid params', async () => {
7855

7956
// NOTE: this test is blocked until the audit service is available in prod
8057
test('sendAuditLogs with default params', async () => {
81-
fetch.mockReturnValue(mockResponse)
58+
setFetchMock(true, 200, {})
8259
const options = {
8360
method: 'POST',
8461
headers: {
@@ -93,7 +70,7 @@ test('sendAuditLogs with default params', async () => {
9370
})
9471

9572
test('should take prod endpoint if calling sendAuditLogs with non-exisiting env', async () => {
96-
fetch.mockReturnValue(mockResponse)
73+
setFetchMock(true, 200, {})
9774
const options = {
9875
method: 'POST',
9976
headers: {
@@ -108,7 +85,7 @@ test('should take prod endpoint if calling sendAuditLogs with non-exisiting env'
10885
})
10986

11087
test('sendAuditLogs error response', async () => {
111-
fetch.mockReturnValue(mockErrorResponse)
88+
setFetchMock(false, 400, {})
11289
const options = {
11390
method: 'POST',
11491
headers: {

test/jest.setup.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ global.getErrorForCallThatShouldThrowAnError = async (callThatShouldThrowAnError
6969
}
7070
}
7171

72+
global.setFetchMock = (ok = true, status = 200, mockData = {}) => {
73+
global.fetch = jest.fn().mockResolvedValue({
74+
ok,
75+
status,
76+
text: () => Promise.resolve(mockData)
77+
})
78+
}
79+
7280
/* global fixtureFile, fixtureJson */
7381

7482
const fixturesFolder = path.join(__dirname, '__fixtures__')

0 commit comments

Comments
 (0)