diff --git a/.eslintrc.json b/.eslintrc.json index 58c258ee6..4d2ac8efc 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -8,7 +8,8 @@ "fixtureJson": true, "fixtureHjson": true, "fixtureYaml": true, - "fakeFileSystem": true + "fakeFileSystem": true, + "setFetchMock": true }, "settings": { "jsdoc": { diff --git a/package.json b/package.json index 12e931c7b..439caca93 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,6 @@ "junk": "^3.1.0", "lodash.clonedeep": "^4.5.0", "node-abort-controller": "^3.1.1", - "node-fetch": "^2.6.7", "open": "^8.4.2", "ora": "^5", "pure-http": "^3", diff --git a/src/lib/audit-logger.js b/src/lib/audit-logger.js index 578fd583b..022ab4eac 100644 --- a/src/lib/audit-logger.js +++ b/src/lib/audit-logger.js @@ -8,7 +8,6 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const fetch = require('node-fetch') const fs = require('fs') const path = require('path') const chalk = require('chalk') diff --git a/test/__fixtures__/pack/2.all.config.json b/test/__fixtures__/pack/2.all.config.json index 9a2065fc8..3ad6be83d 100644 --- a/test/__fixtures__/pack/2.all.config.json +++ b/test/__fixtures__/pack/2.all.config.json @@ -379,7 +379,6 @@ "dependencies": { "@adobe/generator-app-excshell": "^0.2.4", "@adobe/aio-sdk": "^3.0.0", - "node-fetch": "^2.6.0", "core-js": "^3.6.4", "react": "^16.13.1", "react-dom": "^16.13.1", diff --git a/test/__fixtures__/pack/4.all.config.json b/test/__fixtures__/pack/4.all.config.json index fb64691f8..0f83809d9 100644 --- a/test/__fixtures__/pack/4.all.config.json +++ b/test/__fixtures__/pack/4.all.config.json @@ -167,8 +167,7 @@ "version": "0.0.1", "private": true, "dependencies": { - "@adobe/aio-sdk": "^3.0.0", - "node-fetch": "^2.6.0" + "@adobe/aio-sdk": "^3.0.0" }, "devDependencies": { "jest": "^27.2.4", diff --git a/test/__fixtures__/pack/6.all.config.json b/test/__fixtures__/pack/6.all.config.json index 4428e5fc1..928f3c255 100644 --- a/test/__fixtures__/pack/6.all.config.json +++ b/test/__fixtures__/pack/6.all.config.json @@ -167,8 +167,7 @@ "version": "0.0.1-invalidversion", "private": true, "dependencies": { - "@adobe/aio-sdk": "^3.0.0", - "node-fetch": "^2.6.0" + "@adobe/aio-sdk": "^3.0.0" }, "devDependencies": { "jest": "^27.2.4", diff --git a/test/commands/lib/audit-logger.test.js b/test/commands/lib/audit-logger.test.js index 333420998..7e34f8b49 100644 --- a/test/commands/lib/audit-logger.test.js +++ b/test/commands/lib/audit-logger.test.js @@ -11,14 +11,7 @@ governing permissions and limitations under the License. */ const fs = require('fs') -/* eslint-disable no-unused-vars */ -const path = require('path') -/* eslint-disable no-unused-vars */ -const chalk = require('chalk') -const fetch = require('node-fetch') -jest.mock('node-fetch', () => jest.fn()) const auditLogger = require('../../../src/lib/audit-logger') -const { getCliEnv } = require('@adobe/aio-lib-env') jest.mock('fs') jest.mock('chalk', () => ({ @@ -41,28 +34,12 @@ const mockLogEvent = { orgId: 'mockorg' } -const mockResponse = Promise.resolve({ - ok: true, - status: 200, - text: () => { - return {} - } -}) - -const mockErrorResponse = Promise.resolve({ - ok: false, - status: 400, - text: () => { - return {} - } -}) - beforeEach(() => { - fetch.mockReset() + setFetchMock(true, 200, {}) }) test('sendAuditLogs with valid params', async () => { - fetch.mockReturnValue(mockResponse) + setFetchMock(true, 200, {}) const options = { method: 'POST', headers: { @@ -78,7 +55,7 @@ test('sendAuditLogs with valid params', async () => { // NOTE: this test is blocked until the audit service is available in prod test('sendAuditLogs with default params', async () => { - fetch.mockReturnValue(mockResponse) + setFetchMock(true, 200, {}) const options = { method: 'POST', headers: { @@ -93,7 +70,7 @@ test('sendAuditLogs with default params', async () => { }) test('should take prod endpoint if calling sendAuditLogs with non-exisiting env', async () => { - fetch.mockReturnValue(mockResponse) + setFetchMock(true, 200, {}) const options = { method: 'POST', headers: { @@ -108,7 +85,7 @@ test('should take prod endpoint if calling sendAuditLogs with non-exisiting env' }) test('sendAuditLogs error response', async () => { - fetch.mockReturnValue(mockErrorResponse) + setFetchMock(false, 400, {}) const options = { method: 'POST', headers: { diff --git a/test/jest.setup.js b/test/jest.setup.js index 5f3c8f27c..79e240814 100644 --- a/test/jest.setup.js +++ b/test/jest.setup.js @@ -69,6 +69,14 @@ global.getErrorForCallThatShouldThrowAnError = async (callThatShouldThrowAnError } } +global.setFetchMock = (ok = true, status = 200, mockData = {}) => { + global.fetch = jest.fn().mockResolvedValue({ + ok, + status, + text: () => Promise.resolve(mockData) + }) +} + /* global fixtureFile, fixtureJson */ const fixturesFolder = path.join(__dirname, '__fixtures__')