From d69a0e4459849a4164f2c441d1a369c55700168d Mon Sep 17 00:00:00 2001 From: Shazron Abdullah Date: Thu, 7 Oct 2021 00:08:04 +0800 Subject: [PATCH] feat: add proxy support (#480) --- README.md | 62 ++++++++++--------- package.json | 8 ++- src/BaseCommand.js | 2 +- src/lib/app-helper.js | 4 +- test/BaseCommand.test.js | 4 +- test/commands/app/add/service.test.js | 6 +- test/commands/app/delete/service.test.js | 6 +- test/commands/app/init.test.js | 4 +- .../app/list/extension-points.test.js | 4 +- test/commands/app/list/extension.test.js | 4 +- test/commands/app/use.test.js | 6 +- test/commands/lib/app-helper.test.js | 31 +++++----- 12 files changed, 76 insertions(+), 65 deletions(-) diff --git a/README.md b/README.md index 061bf020..db9cefc3 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ OPTIONS --version Show version ``` -_See code: [src/commands/app/index.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/index.js)_ +_See code: [src/commands/app/index.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/index.js)_ ## `aio app:add` @@ -88,7 +88,7 @@ OPTIONS --version Show version ``` -_See code: [src/commands/app/add/index.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/add/index.js)_ +_See code: [src/commands/app/add/index.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/add/index.js)_ ## `aio app:add:action` @@ -112,7 +112,7 @@ ALIASES $ aio app:add:actions ``` -_See code: [src/commands/app/add/action.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/add/action.js)_ +_See code: [src/commands/app/add/action.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/add/action.js)_ ## `aio app:add:ci` @@ -130,7 +130,7 @@ OPTIONS --version Show version ``` -_See code: [src/commands/app/add/ci.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/add/ci.js)_ +_See code: [src/commands/app/add/ci.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/add/ci.js)_ ## `aio app:add:event` @@ -154,7 +154,7 @@ ALIASES $ aio app:add:events ``` -_See code: [src/commands/app/add/event.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/add/event.js)_ +_See code: [src/commands/app/add/event.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/add/event.js)_ ## `aio app:add:extension` @@ -179,7 +179,7 @@ ALIASES $ aio app:add:extensions ``` -_See code: [src/commands/app/add/extension.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/add/extension.js)_ +_See code: [src/commands/app/add/extension.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/add/extension.js)_ ## `aio app:add:service` @@ -200,7 +200,7 @@ ALIASES $ aio app:add:services ``` -_See code: [src/commands/app/add/service.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/add/service.js)_ +_See code: [src/commands/app/add/service.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/add/service.js)_ ## `aio app:add:web-assets` @@ -221,7 +221,7 @@ OPTIONS --version Show version ``` -_See code: [src/commands/app/add/web-assets.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/add/web-assets.js)_ +_See code: [src/commands/app/add/web-assets.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/add/web-assets.js)_ ## `aio app:build` @@ -260,11 +260,13 @@ OPTIONS --[no-]web-assets [default: true] Build web-assets if any + --web-optimize [default: false] Enable optimization (minification) of js/css/html + DESCRIPTION This will always force a rebuild unless --no-force-build is set. ``` -_See code: [src/commands/app/build.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/build.js)_ +_See code: [src/commands/app/build.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/build.js)_ ## `aio app:create [PATH]` @@ -286,7 +288,7 @@ OPTIONS --version Show version ``` -_See code: [src/commands/app/create.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/create.js)_ +_See code: [src/commands/app/create.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/create.js)_ ## `aio app:delete` @@ -303,7 +305,7 @@ OPTIONS --version Show version ``` -_See code: [src/commands/app/delete/index.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/delete/index.js)_ +_See code: [src/commands/app/delete/index.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/delete/index.js)_ ## `aio app:delete:action [ACTION-NAME]` @@ -328,7 +330,7 @@ ALIASES $ aio app:delete:actions ``` -_See code: [src/commands/app/delete/action.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/delete/action.js)_ +_See code: [src/commands/app/delete/action.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/delete/action.js)_ ## `aio app:delete:ci` @@ -347,7 +349,7 @@ OPTIONS --version Show version ``` -_See code: [src/commands/app/delete/ci.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/delete/ci.js)_ +_See code: [src/commands/app/delete/ci.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/delete/ci.js)_ ## `aio app:delete:event [EVENT-ACTION-NAME]` @@ -372,7 +374,7 @@ ALIASES $ aio app:delete:events ``` -_See code: [src/commands/app/delete/event.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/delete/event.js)_ +_See code: [src/commands/app/delete/event.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/delete/event.js)_ ## `aio app:delete:extension` @@ -397,7 +399,7 @@ ALIASES $ aio app:delete:extensions ``` -_See code: [src/commands/app/delete/extension.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/delete/extension.js)_ +_See code: [src/commands/app/delete/extension.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/delete/extension.js)_ ## `aio app:delete:service` @@ -418,7 +420,7 @@ ALIASES $ aio app:delete:services ``` -_See code: [src/commands/app/delete/service.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/delete/service.js)_ +_See code: [src/commands/app/delete/service.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/delete/service.js)_ ## `aio app:delete:web-assets` @@ -437,7 +439,7 @@ OPTIONS --version Show version ``` -_See code: [src/commands/app/delete/web-assets.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/delete/web-assets.js)_ +_See code: [src/commands/app/delete/web-assets.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/delete/web-assets.js)_ ## `aio app:deploy` @@ -489,11 +491,13 @@ OPTIONS --[no-]web-assets [default: true] Deploy web-assets if any + --web-optimize [default: false] Enable optimization (minification) of web js/css/html + DESCRIPTION This will always force a rebuild unless --no-force-build is set. ``` -_See code: [src/commands/app/deploy.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/deploy.js)_ +_See code: [src/commands/app/deploy.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/deploy.js)_ ## `aio app:get-url [ACTION]` @@ -514,7 +518,7 @@ OPTIONS --version Show version ``` -_See code: [src/commands/app/get-url.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/get-url.js)_ +_See code: [src/commands/app/get-url.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/get-url.js)_ ## `aio app:info` @@ -536,7 +540,7 @@ OPTIONS --version Show version ``` -_See code: [src/commands/app/info.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/info.js)_ +_See code: [src/commands/app/info.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/info.js)_ ## `aio app:init [PATH]` @@ -570,7 +574,7 @@ OPTIONS --version Show version ``` -_See code: [src/commands/app/init.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/init.js)_ +_See code: [src/commands/app/init.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/init.js)_ ## `aio app:list` @@ -587,7 +591,7 @@ OPTIONS --version Show version ``` -_See code: [src/commands/app/list/index.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/list/index.js)_ +_See code: [src/commands/app/list/index.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/list/index.js)_ ## `aio app:list:extension` @@ -611,7 +615,7 @@ ALIASES $ aio app:list:extensions ``` -_See code: [src/commands/app/list/extension.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/list/extension.js)_ +_See code: [src/commands/app/list/extension.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/list/extension.js)_ ## `aio app:list:extension-points` @@ -635,7 +639,7 @@ ALIASES $ aio app:list:extension-points ``` -_See code: [src/commands/app/list/extension-points.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/list/extension-points.js)_ +_See code: [src/commands/app/list/extension-points.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/list/extension-points.js)_ ## `aio app:logs` @@ -659,7 +663,7 @@ OPTIONS --version Show version ``` -_See code: [src/commands/app/logs.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/logs.js)_ +_See code: [src/commands/app/logs.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/logs.js)_ ## `aio app:run` @@ -682,7 +686,7 @@ OPTIONS --version Show version ``` -_See code: [src/commands/app/run.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/run.js)_ +_See code: [src/commands/app/run.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/run.js)_ ## `aio app:test` @@ -716,7 +720,7 @@ DESCRIPTION If the extension has a hook called 'test' in its 'ext.config.yaml', the script specified will be run instead. ``` -_See code: [src/commands/app/test.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/test.js)_ +_See code: [src/commands/app/test.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/test.js)_ ## `aio app:undeploy` @@ -742,7 +746,7 @@ OPTIONS --[no-]web-assets [default: true] Undeploy web-assets if any ``` -_See code: [src/commands/app/undeploy.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/undeploy.js)_ +_See code: [src/commands/app/undeploy.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/undeploy.js)_ ## `aio app:use [CONFIG_FILE_PATH]` @@ -802,5 +806,5 @@ DESCRIPTION page in https://console.adobe.io ``` -_See code: [src/commands/app/use.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.0/src/commands/app/use.js)_ +_See code: [src/commands/app/use.js](https://github.com/adobe/aio-cli-plugin-app/blob/8.1.1-next.2/src/commands/app/use.js)_ diff --git a/package.json b/package.json index afd44732..5b8ac5fc 100644 --- a/package.json +++ b/package.json @@ -6,14 +6,15 @@ "bugs": "https://github.com/adobe/aio-cli-plugin-app/issues", "dependencies": { "@adobe/aio-cli-lib-app-config": "^0.2.1", + "@adobe/aio-cli-lib-console": "^3.0.0", "@adobe/aio-lib-core-config": "^2.0.0", "@adobe/aio-lib-core-logging": "^1.1.0", + "@adobe/aio-lib-core-networking": "^2.0.0", "@adobe/aio-lib-env": "^1.1.0", - "@adobe/aio-lib-ims": "^4.1.0", - "@adobe/aio-lib-runtime": "^3.0.1", + "@adobe/aio-lib-ims": "^5.0.0", + "@adobe/aio-lib-runtime": "^3.1.0", "@adobe/aio-lib-web": "^5.0.0", "@adobe/generator-aio-app": "^3.0.0", - "@adobe/generator-aio-console": "^2.2.1", "@oclif/command": "^1.5.11", "@oclif/config": "^1.12.9", "@oclif/plugin-help": "^2.2.3", @@ -42,6 +43,7 @@ "yeoman-environment": "^3.2.0" }, "devDependencies": { + "@adobe/aio-lib-test-proxy": "^1.0.0", "@adobe/eslint-config-aio-lib-config": "^1.3.0", "@oclif/dev-cli": "^1.21.3", "@types/jest": "^26.0.10", diff --git a/src/BaseCommand.js b/src/BaseCommand.js index 948f837e..a374ceda 100644 --- a/src/BaseCommand.js +++ b/src/BaseCommand.js @@ -18,7 +18,7 @@ const loadConfig = require('@adobe/aio-cli-lib-app-config') const inquirer = require('inquirer') const { CONSOLE_API_KEYS, APPLICATION_CONFIG_KEY, EXTENSIONS_CONFIG_KEY } = require('./lib/defaults') const { getCliInfo } = require('./lib/app-helper') -const LibConsoleCLI = require('@adobe/generator-aio-console/lib/console-cli') +const LibConsoleCLI = require('@adobe/aio-cli-lib-console') const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-app', { provider: 'debug' }) const { diff --git a/src/lib/app-helper.js b/src/lib/app-helper.js index a7b46915..3794e899 100644 --- a/src/lib/app-helper.js +++ b/src/lib/app-helper.js @@ -16,7 +16,7 @@ const which = require('which') const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-app:lib-app-helper', { provider: 'debug' }) const { getToken, context } = require('@adobe/aio-lib-ims') const { CLI } = require('@adobe/aio-lib-ims/src/context') -const fetch = require('node-fetch') +const { createFetch } = require('@adobe/aio-lib-core-networking') const chalk = require('chalk') const aioConfig = require('@adobe/aio-lib-core-config') const { AIO_CONFIG_WORKSPACE_SERVICES, AIO_CONFIG_ORG_SERVICES } = require('./defaults') @@ -255,6 +255,7 @@ async function downloadOWJar (url, outFile) { aioLogger.debug(`downloadOWJar - url: ${url} outFile: ${outFile}`) let response try { + const fetch = createFetch() response = await fetch(url) } catch (e) { aioLogger.debug(`connection error while downloading '${url}'`, e) @@ -284,6 +285,7 @@ async function waitForOpenWhiskReadiness (host, endTime, period, timeout, waitFu let ok try { + const fetch = createFetch() const response = await fetch(host + '/api/v1') ok = response.ok } catch (e) { diff --git a/test/BaseCommand.test.js b/test/BaseCommand.test.js index 4b80148c..7ea08081 100644 --- a/test/BaseCommand.test.js +++ b/test/BaseCommand.test.js @@ -26,8 +26,8 @@ jest.mock('@adobe/aio-lib-env') jest.mock('@adobe/aio-lib-ims') const { getToken } = require('@adobe/aio-lib-ims') -jest.mock('@adobe/generator-aio-console/lib/console-cli.js') -const LibConsoleCLI = require('@adobe/generator-aio-console/lib/console-cli.js') +jest.mock('@adobe/aio-cli-lib-console') +const LibConsoleCLI = require('@adobe/aio-cli-lib-console') LibConsoleCLI.init.mockResolvedValue({}) const TheCommand = require('../src/BaseCommand') diff --git a/test/commands/app/add/service.test.js b/test/commands/app/add/service.test.js index 8a3519b2..2200ff08 100644 --- a/test/commands/app/add/service.test.js +++ b/test/commands/app/add/service.test.js @@ -9,10 +9,10 @@ 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 consoleDataMocks = require('@adobe/generator-aio-console/test/data-mocks') +const consoleDataMocks = require('@adobe/aio-cli-lib-console/test/data-mocks') -jest.mock('@adobe/generator-aio-console/lib/console-cli.js') -const LibConsoleCLI = require('@adobe/generator-aio-console/lib/console-cli.js') +jest.mock('@adobe/aio-cli-lib-console') +const LibConsoleCLI = require('@adobe/aio-cli-lib-console') const mockConsoleCLIInstance = { getWorkspaces: jest.fn(), promptForSelectWorkspace: jest.fn(), diff --git a/test/commands/app/delete/service.test.js b/test/commands/app/delete/service.test.js index 5339770f..019f718a 100644 --- a/test/commands/app/delete/service.test.js +++ b/test/commands/app/delete/service.test.js @@ -9,10 +9,10 @@ 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 consoleDataMocks = require('@adobe/generator-aio-console/test/data-mocks') +const consoleDataMocks = require('@adobe/aio-cli-lib-console/test/data-mocks') -jest.mock('@adobe/generator-aio-console/lib/console-cli.js') -const LibConsoleCLI = require('@adobe/generator-aio-console/lib/console-cli.js') +jest.mock('@adobe/aio-cli-lib-console') +const LibConsoleCLI = require('@adobe/aio-cli-lib-console') const mockConsoleCLIInstance = { getEnabledServicesForOrg: jest.fn(), promptForRemoveServiceSubscriptions: jest.fn(), diff --git a/test/commands/app/init.test.js b/test/commands/app/init.test.js index 3a0994a6..dc1b336d 100644 --- a/test/commands/app/init.test.js +++ b/test/commands/app/init.test.js @@ -34,8 +34,8 @@ yeoman.createEnv.mockReturnValue({ jest.mock('@adobe/aio-lib-ims') // mock console calls -jest.mock('@adobe/generator-aio-console/lib/console-cli.js') -const LibConsoleCLI = require('@adobe/generator-aio-console/lib/console-cli.js') +jest.mock('@adobe/aio-cli-lib-console') +const LibConsoleCLI = require('@adobe/aio-cli-lib-console') const mockConsoleCLIInstance = { getEnabledServicesForOrg: jest.fn(), promptForSelectOrganization: jest.fn(), diff --git a/test/commands/app/list/extension-points.test.js b/test/commands/app/list/extension-points.test.js index 4ba07603..30aa6bf1 100644 --- a/test/commands/app/list/extension-points.test.js +++ b/test/commands/app/list/extension-points.test.js @@ -15,8 +15,8 @@ const BaseCommand = require('../../../../src/BaseCommand') const yaml = require('js-yaml') const dataMocks = require('../../../data-mocks/config-loader') -jest.mock('@adobe/generator-aio-console/lib/console-cli.js') -const LibConsoleCLI = require('@adobe/generator-aio-console/lib/console-cli.js') +jest.mock('@adobe/aio-cli-lib-console') +const LibConsoleCLI = require('@adobe/aio-cli-lib-console') const mockConsoleCLIInstance = {} LibConsoleCLI.init.mockResolvedValue(mockConsoleCLIInstance) diff --git a/test/commands/app/list/extension.test.js b/test/commands/app/list/extension.test.js index 9f4bfb17..01dd9764 100644 --- a/test/commands/app/list/extension.test.js +++ b/test/commands/app/list/extension.test.js @@ -15,8 +15,8 @@ const BaseCommand = require('../../../../src/BaseCommand') const yaml = require('js-yaml') const dataMocks = require('../../../data-mocks/config-loader') -jest.mock('@adobe/generator-aio-console/lib/console-cli.js') -const LibConsoleCLI = require('@adobe/generator-aio-console/lib/console-cli.js') +jest.mock('@adobe/aio-cli-lib-console') +const LibConsoleCLI = require('@adobe/aio-cli-lib-console') const mockConsoleCLIInstance = {} LibConsoleCLI.init.mockResolvedValue(mockConsoleCLIInstance) diff --git a/test/commands/app/use.test.js b/test/commands/app/use.test.js index dbd31a83..b7042f89 100644 --- a/test/commands/app/use.test.js +++ b/test/commands/app/use.test.js @@ -20,9 +20,9 @@ const mockPrompt = jest.fn() inquirer.createPromptModule.mockReturnValue(mockPrompt) // mock LibConsoleCLI -const consoleDataMocks = require('@adobe/generator-aio-console/test/data-mocks') -jest.mock('@adobe/generator-aio-console/lib/console-cli.js') -const LibConsoleCLI = require('@adobe/generator-aio-console/lib/console-cli.js') +const consoleDataMocks = require('@adobe/aio-cli-lib-console/test/data-mocks') +jest.mock('@adobe/aio-cli-lib-console') +const LibConsoleCLI = require('@adobe/aio-cli-lib-console') const mockConsoleCLIInstance = { getWorkspaces: jest.fn(), promptForSelectWorkspace: jest.fn(), diff --git a/test/commands/lib/app-helper.test.js b/test/commands/lib/app-helper.test.js index 276a0bc0..93f9b276 100644 --- a/test/commands/lib/app-helper.test.js +++ b/test/commands/lib/app-helper.test.js @@ -13,8 +13,12 @@ governing permissions and limitations under the License. // unmock to test proper returned urls from getActionUrls jest.unmock('@adobe/aio-lib-runtime') +const mockFetch = jest.fn() +jest.mock('@adobe/aio-lib-core-networking', () => ({ + createFetch: jest.fn(() => mockFetch) +})) + jest.mock('@adobe/aio-lib-core-config') -jest.mock('node-fetch') jest.mock('execa') jest.mock('process') jest.mock('fs-extra') // do not touch the real fs @@ -25,7 +29,6 @@ const which = require('which') const fs = require('fs-extra') const execa = require('execa') const appHelper = require('../../../src/lib/app-helper') -const fetch = require('node-fetch') const aioConfig = require('@adobe/aio-lib-core-config') const libEnv = require('@adobe/aio-lib-env') const libIms = require('@adobe/aio-lib-ims') @@ -34,11 +37,11 @@ beforeEach(() => { Object.defineProperty(process, 'platform', { value: 'linux' }) execa.mockReset() execa.command.mockReset() - fetch.mockReset() aioConfig.get.mockReset() aioConfig.set.mockReset() libEnv.getCliEnv.mockReset() libIms.getToken.mockReset() + mockFetch.mockReset() }) const getMockConfig = require('../../data-mocks/config-loader') @@ -389,7 +392,7 @@ test('downloadOWJar failed (server has response, but not ok)', async () => { ok: false, statusText: 'some error' } - fetch.mockReturnValueOnce(response) + mockFetch.mockResolvedValueOnce(response) const url = 'https://some.url' const result = appHelper.downloadOWJar(url, 'foo/bar') @@ -398,7 +401,7 @@ test('downloadOWJar failed (server has response, but not ok)', async () => { test('downloadOWJar failed (no server response, fetch exception)', async () => { const err = new Error('some fetch error') - fetch.mockRejectedValueOnce(err) + mockFetch.mockRejectedValueOnce(err) const url = 'https://some.url' const result = appHelper.downloadOWJar(url, 'foo/bar') @@ -428,7 +431,7 @@ test('downloadOWJar ok', async () => { }) } }) - fetch.mockReturnValueOnce(response) + mockFetch.mockResolvedValueOnce(response) const result = appHelper.downloadOWJar(url, fileToWrite) await expect(result).resolves.toBeUndefined() @@ -451,14 +454,14 @@ test('downloadOWJar (server connected ok, streaming error)', async () => { const url = 'https://some.url' const fileToWrite = 'foo/bar' - fetch.mockReturnValueOnce(response) + mockFetch.mockResolvedValueOnce(response) const result = appHelper.downloadOWJar(url, fileToWrite) await expect(result).rejects.toEqual(streamError) }) test('runOpenWhiskJar ok', async () => { - fetch.mockReturnValue({ ok: true }) + mockFetch.mockResolvedValue({ ok: true }) execa.mockReturnValue({ stdout: jest.fn() }) const result = appHelper.runOpenWhiskJar('jar', 'conf') @@ -466,12 +469,12 @@ test('runOpenWhiskJar ok', async () => { await expect(result).resolves.toEqual({ proc: expect.any(Object) }) - expect(fetch).toHaveBeenCalledTimes(1) + expect(mockFetch).toHaveBeenCalledTimes(1) expect(execa).toHaveBeenCalledWith('java', expect.arrayContaining(['jar', 'conf']), {}) }) test('runOpenWhiskJar with AIO_OW_JVM_ARGS env var is passed to execa', async () => { - fetch.mockReturnValue({ ok: true }) + mockFetch.mockResolvedValue({ ok: true }) execa.mockReturnValue({ stdout: jest.fn() }) aioConfig.get.mockReturnValueOnce('arg1 arg2') @@ -481,7 +484,7 @@ test('runOpenWhiskJar with AIO_OW_JVM_ARGS env var is passed to execa', async () await expect(result).resolves.toEqual({ proc: expect.any(Object) }) - expect(fetch).toHaveBeenCalledTimes(1) + expect(mockFetch).toHaveBeenCalledTimes(1) expect(execa).toHaveBeenCalledWith('java', expect.arrayContaining(['arg1', 'arg2', 'jar', 'conf']), {}) }) @@ -497,7 +500,7 @@ test('waitForOpenWhiskReadiness timeout', async () => { const result = appHelper.waitForOpenWhiskReadiness(host, endTime, period, timeout, waitFunc) await expect(result).rejects.toEqual(new Error(`local openwhisk stack startup timed out: ${timeout}ms`)) - expect(fetch).toHaveBeenCalledTimes(0) + expect(mockFetch).toHaveBeenCalledTimes(0) expect(waitFunc).toHaveBeenCalledTimes(0) }) @@ -510,14 +513,14 @@ test('waitForOpenWhiskReadiness (fail, retry, then success)', async () => { const waitFunc = jest.fn((_period) => { expect(_period).toEqual(period) }) - fetch + mockFetch .mockRejectedValueOnce(new Error('some error')) // first fail (fetch exception) .mockRejectedValueOnce({ ok: false }) // second fail (response not ok) .mockResolvedValue({ ok: true }) // finally success const result = appHelper.waitForOpenWhiskReadiness(host, endTime, period, timeout, waitFunc) await expect(result).resolves.not.toBeDefined() - expect(fetch).toHaveBeenCalledTimes(3) + expect(mockFetch).toHaveBeenCalledTimes(3) expect(waitFunc).toHaveBeenCalledTimes(2) })