From 0f2c313752a0f8860a4fe19a9ad6b00e7c3a5157 Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Thu, 9 Jun 2022 10:53:18 -0400 Subject: [PATCH] refactor: conditionally install node-fetch globally vs. node.js v18+ --- .eslintrc.cjs | 6 ++++++ src/index.js | 8 ++++++++ .../handler-runner/docker-runner/DockerContainer.js | 1 - src/lambda/handler-runner/java-runner/JavaRunner.js | 1 - src/lambda/routes/invocations/invocationsRoute.js | 1 - .../environmentVariables/environmentVariables.test.js | 1 - .../endToEnd/optionParameters/optionParameters.test.js | 1 - tests/endToEnd/starRoutes/starRoutes.test.js | 1 - .../starRoutesWithProperties/starRoutes.test.js | 1 - tests/endToEnd/trailingSlash/trailingSlash.test.js | 1 - tests/integration/artifact/local/localArtifact.test.js | 1 - tests/integration/authorizer/authorizer.test.js | 1 - .../authenticationCustomVariable.test.js | 1 - .../integration/docker/access-host/accessHost.test.js | 1 - tests/integration/docker/artifact/artifact.test.js | 1 - tests/integration/docker/go/go1.x/dockerGo1.x.test.js | 1 - tests/integration/docker/layers/dockerLayers.test.js | 1 - .../integration/docker/multiple/dockerMultiple.test.js | 1 - .../docker/nodejs/nodejs10.x/dockerNodejs10.x.test.js | 1 - .../docker/nodejs/nodejs12.x/dockerNodejs12.x.test.js | 1 - .../docker/nodejs/nodejs8.10/dockerNodejs8.10.test.js | 1 - .../integration/docker/provided/dockerProvided.test.js | 1 - .../docker/python/python3.6/dockerPython3.6.test.js | 1 - .../docker/python/python3.7/dockerPython3.7.test.js | 1 - .../docker/python/python3.8/dockerPython3.8.test.js | 1 - .../docker/ruby/ruby2.7/dockerRuby2.7.test.js | 1 - tests/integration/go/go1.x/go1.x.test.js | 1 - tests/integration/groovy/groovy.test.js | 1 - tests/integration/handler/handlerPayload.test.js | 1 - .../httpApi-cors-default/httpApi-cors-default.test.js | 1 - tests/integration/httpApi-cors/httpApi-cors.test.js | 1 - .../httpApi-headers/httpApi-headers.test.js | 1 - tests/integration/java/java.test.js | 1 - .../integration/jwt-authorizer/jwt-authorizer.test.js | 1 - tests/integration/kotlin/kotlin.test.js | 1 - .../lambda-integration/lambdaIntegration.test.js | 1 - tests/integration/lambda-invoke/lambdaInvoke.test.js | 1 - .../lambda-invoke/lambdaInvokeAsync.test.js | 1 - .../override-authorizer/override-authorizer.test.js | 1 - .../python/python-big-json/python-big-json.test.js | 1 - tests/integration/python/python3/python3.test.js | 1 - tests/integration/ruby/ruby.test.js | 1 - tests/integration/scala/scala.test.js | 1 - tests/integration/uncategorized/uncategorized.test.js | 1 - tests/mochaGlobalSetup.cjs | 10 +++++++++- tests/scenario/apollo-server-lambda.test.js | 1 - tests/scenario/docker-in-docker.test.js | 1 - tests/scenario/docker-serverless-webpack.test.js | 1 - tests/scenario/serverless-plugin-typescript.test.js | 1 - tests/scenario/serverless-webpack.test.js | 1 - 50 files changed, 23 insertions(+), 48 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 26d60b91f..bc0c9f32b 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -2,6 +2,7 @@ module.exports = { env: { + es2021: true, jest: true, }, @@ -12,6 +13,11 @@ module.exports = { ], globals: { + // TODO remove `node-fetch` module with node.js v18+ support + // TODO file bug with eslint? those should be global now + fetch: true, + Headers: true, + // RUN_TEST_AGAINST_AWS: true, TEST_BASE_URL: true, }, diff --git a/src/index.js b/src/index.js index dbc547fbe..b3c035b2f 100755 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,12 @@ // TODO remove with node.js v16.9+ support import 'object.hasown/auto' +// install global fetch +// TODO remove `node-fetch` module and use global built-in with node.js v18+ support +if (globalThis.fetch === undefined) { + const { default: fetch, Headers } = await import('node-fetch') + globalThis.fetch = fetch + globalThis.Headers = Headers +} + export { default } from './ServerlessOffline.js' diff --git a/src/lambda/handler-runner/docker-runner/DockerContainer.js b/src/lambda/handler-runner/docker-runner/DockerContainer.js index c875b353b..a21d4fa32 100644 --- a/src/lambda/handler-runner/docker-runner/DockerContainer.js +++ b/src/lambda/handler-runner/docker-runner/DockerContainer.js @@ -8,7 +8,6 @@ import { Lambda } from 'aws-sdk' import { execa } from 'execa' import { ensureDir, pathExists } from 'fs-extra' import jszip from 'jszip' -import fetch from 'node-fetch' import pRetry from 'p-retry' import DockerImage from './DockerImage.js' diff --git a/src/lambda/handler-runner/java-runner/JavaRunner.js b/src/lambda/handler-runner/java-runner/JavaRunner.js index e5b4ccbc7..20bc08e23 100644 --- a/src/lambda/handler-runner/java-runner/JavaRunner.js +++ b/src/lambda/handler-runner/java-runner/JavaRunner.js @@ -1,6 +1,5 @@ import { EOL } from 'node:os' import process from 'node:process' -import fetch from 'node-fetch' import { log } from '@serverless/utils/log.js' import { invokeJavaLocal } from 'java-invoke-local' diff --git a/src/lambda/routes/invocations/invocationsRoute.js b/src/lambda/routes/invocations/invocationsRoute.js index a1e11b9c9..1039304c5 100644 --- a/src/lambda/routes/invocations/invocationsRoute.js +++ b/src/lambda/routes/invocations/invocationsRoute.js @@ -1,5 +1,4 @@ import { Buffer } from 'node:buffer' -import { Headers } from 'node-fetch' import InvocationsController from './InvocationsController.js' const { parse } = JSON diff --git a/tests/endToEnd/environmentVariables/environmentVariables.test.js b/tests/endToEnd/environmentVariables/environmentVariables.test.js index b12d796d4..f7681d7d9 100644 --- a/tests/endToEnd/environmentVariables/environmentVariables.test.js +++ b/tests/endToEnd/environmentVariables/environmentVariables.test.js @@ -2,7 +2,6 @@ import assert from 'node:assert' import { dirname, resolve } from 'node:path' import { env } from 'node:process' import { fileURLToPath } from 'node:url' -import fetch from 'node-fetch' import { joinUrl, setup, diff --git a/tests/endToEnd/optionParameters/optionParameters.test.js b/tests/endToEnd/optionParameters/optionParameters.test.js index eadf8b4ec..fe8129159 100644 --- a/tests/endToEnd/optionParameters/optionParameters.test.js +++ b/tests/endToEnd/optionParameters/optionParameters.test.js @@ -2,7 +2,6 @@ import assert from 'node:assert' import { dirname, resolve } from 'node:path' import { env } from 'node:process' import { fileURLToPath } from 'node:url' -import fetch from 'node-fetch' import { joinUrl, setup, diff --git a/tests/endToEnd/starRoutes/starRoutes.test.js b/tests/endToEnd/starRoutes/starRoutes.test.js index f6f0775b3..d8d48c954 100644 --- a/tests/endToEnd/starRoutes/starRoutes.test.js +++ b/tests/endToEnd/starRoutes/starRoutes.test.js @@ -2,7 +2,6 @@ import assert from 'node:assert' import { dirname, resolve } from 'node:path' import { env } from 'node:process' import { fileURLToPath } from 'node:url' -import fetch from 'node-fetch' import { joinUrl, setup, diff --git a/tests/endToEnd/starRoutesWithProperties/starRoutes.test.js b/tests/endToEnd/starRoutesWithProperties/starRoutes.test.js index 83e78ae69..c48d1395d 100644 --- a/tests/endToEnd/starRoutesWithProperties/starRoutes.test.js +++ b/tests/endToEnd/starRoutesWithProperties/starRoutes.test.js @@ -2,7 +2,6 @@ import assert from 'node:assert' import { dirname, resolve } from 'node:path' import { env } from 'node:process' import { fileURLToPath } from 'node:url' -import fetch from 'node-fetch' import { joinUrl, setup, diff --git a/tests/endToEnd/trailingSlash/trailingSlash.test.js b/tests/endToEnd/trailingSlash/trailingSlash.test.js index a07c562b7..ea972cefb 100644 --- a/tests/endToEnd/trailingSlash/trailingSlash.test.js +++ b/tests/endToEnd/trailingSlash/trailingSlash.test.js @@ -2,7 +2,6 @@ import assert from 'node:assert' import { dirname, resolve } from 'node:path' import { env } from 'node:process' import { fileURLToPath } from 'node:url' -import fetch from 'node-fetch' import { joinUrl, setup, diff --git a/tests/integration/artifact/local/localArtifact.test.js b/tests/integration/artifact/local/localArtifact.test.js index 8c203fa2c..8cf9d7588 100644 --- a/tests/integration/artifact/local/localArtifact.test.js +++ b/tests/integration/artifact/local/localArtifact.test.js @@ -2,7 +2,6 @@ import assert from 'node:assert' import { dirname, resolve } from 'node:path' import { env } from 'node:process' import { fileURLToPath } from 'node:url' -import fetch from 'node-fetch' import { compressArtifact, joinUrl, diff --git a/tests/integration/authorizer/authorizer.test.js b/tests/integration/authorizer/authorizer.test.js index 31860f871..224191719 100644 --- a/tests/integration/authorizer/authorizer.test.js +++ b/tests/integration/authorizer/authorizer.test.js @@ -5,7 +5,6 @@ import assert from 'node:assert' import { dirname, resolve } from 'node:path' import { env } from 'node:process' import { fileURLToPath } from 'node:url' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../_testHelpers/index.js' const __dirname = dirname(fileURLToPath(import.meta.url)) diff --git a/tests/integration/custom-authentication/authenticationCustomVariable.test.js b/tests/integration/custom-authentication/authenticationCustomVariable.test.js index 5c0934cfb..c473a2a2f 100644 --- a/tests/integration/custom-authentication/authenticationCustomVariable.test.js +++ b/tests/integration/custom-authentication/authenticationCustomVariable.test.js @@ -2,7 +2,6 @@ import assert from 'node:assert' import { dirname, resolve } from 'node:path' import { env } from 'node:process' import { fileURLToPath } from 'node:url' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../_testHelpers/index.js' const __dirname = dirname(fileURLToPath(import.meta.url)) diff --git a/tests/integration/docker/access-host/accessHost.test.js b/tests/integration/docker/access-host/accessHost.test.js index 926df930f..56312c03e 100644 --- a/tests/integration/docker/access-host/accessHost.test.js +++ b/tests/integration/docker/access-host/accessHost.test.js @@ -2,7 +2,6 @@ import assert from 'node:assert' import { resolve } from 'node:path' import { env } from 'node:process' import { Server } from '@hapi/hapi' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../../_testHelpers/index.js' // "Could not find 'Docker', skipping 'Docker' tests." diff --git a/tests/integration/docker/artifact/artifact.test.js b/tests/integration/docker/artifact/artifact.test.js index e35bce3c1..f337ee4b5 100644 --- a/tests/integration/docker/artifact/artifact.test.js +++ b/tests/integration/docker/artifact/artifact.test.js @@ -1,7 +1,6 @@ import assert from 'node:assert' import { resolve } from 'node:path' import { env } from 'node:process' -import fetch from 'node-fetch' import { compressArtifact, joinUrl, diff --git a/tests/integration/docker/go/go1.x/dockerGo1.x.test.js b/tests/integration/docker/go/go1.x/dockerGo1.x.test.js index 3eab5723e..a911df802 100644 --- a/tests/integration/docker/go/go1.x/dockerGo1.x.test.js +++ b/tests/integration/docker/go/go1.x/dockerGo1.x.test.js @@ -1,7 +1,6 @@ import assert from 'node:assert' import { resolve } from 'node:path' import { env } from 'node:process' -import fetch from 'node-fetch' import { joinUrl, buildInContainer, diff --git a/tests/integration/docker/layers/dockerLayers.test.js b/tests/integration/docker/layers/dockerLayers.test.js index 377f7957a..a126f3805 100644 --- a/tests/integration/docker/layers/dockerLayers.test.js +++ b/tests/integration/docker/layers/dockerLayers.test.js @@ -1,7 +1,6 @@ import assert from 'node:assert' import { resolve } from 'node:path' import { env } from 'node:process' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../../_testHelpers/index.js' // "Could not find 'Docker', skipping 'Docker' tests." diff --git a/tests/integration/docker/multiple/dockerMultiple.test.js b/tests/integration/docker/multiple/dockerMultiple.test.js index a50352cc8..834ebd9e1 100644 --- a/tests/integration/docker/multiple/dockerMultiple.test.js +++ b/tests/integration/docker/multiple/dockerMultiple.test.js @@ -1,7 +1,6 @@ import assert from 'node:assert' import { resolve } from 'node:path' import { env } from 'node:process' -import fetch from 'node-fetch' import semver from 'semver' import { joinUrl, setup, teardown } from '../../_testHelpers/index.js' diff --git a/tests/integration/docker/nodejs/nodejs10.x/dockerNodejs10.x.test.js b/tests/integration/docker/nodejs/nodejs10.x/dockerNodejs10.x.test.js index f5e228934..c18a2bbf7 100644 --- a/tests/integration/docker/nodejs/nodejs10.x/dockerNodejs10.x.test.js +++ b/tests/integration/docker/nodejs/nodejs10.x/dockerNodejs10.x.test.js @@ -1,7 +1,6 @@ import assert from 'node:assert' import { resolve } from 'node:path' import { env } from 'node:process' -import fetch from 'node-fetch' import semver from 'semver' import { joinUrl, setup, teardown } from '../../../_testHelpers/index.js' diff --git a/tests/integration/docker/nodejs/nodejs12.x/dockerNodejs12.x.test.js b/tests/integration/docker/nodejs/nodejs12.x/dockerNodejs12.x.test.js index bb95f5f67..814430ffd 100644 --- a/tests/integration/docker/nodejs/nodejs12.x/dockerNodejs12.x.test.js +++ b/tests/integration/docker/nodejs/nodejs12.x/dockerNodejs12.x.test.js @@ -1,7 +1,6 @@ import assert from 'node:assert' import { resolve } from 'node:path' import { env } from 'node:process' -import fetch from 'node-fetch' import semver from 'semver' import { joinUrl, setup, teardown } from '../../../_testHelpers/index.js' diff --git a/tests/integration/docker/nodejs/nodejs8.10/dockerNodejs8.10.test.js b/tests/integration/docker/nodejs/nodejs8.10/dockerNodejs8.10.test.js index 3909d41b0..d3a23a0a8 100644 --- a/tests/integration/docker/nodejs/nodejs8.10/dockerNodejs8.10.test.js +++ b/tests/integration/docker/nodejs/nodejs8.10/dockerNodejs8.10.test.js @@ -1,7 +1,6 @@ import assert from 'node:assert' import { resolve } from 'node:path' import { env } from 'node:process' -import fetch from 'node-fetch' import semver from 'semver' import { joinUrl, setup, teardown } from '../../../_testHelpers/index.js' diff --git a/tests/integration/docker/provided/dockerProvided.test.js b/tests/integration/docker/provided/dockerProvided.test.js index 524e63f8a..437d5919b 100644 --- a/tests/integration/docker/provided/dockerProvided.test.js +++ b/tests/integration/docker/provided/dockerProvided.test.js @@ -1,7 +1,6 @@ import assert from 'node:assert' import { resolve } from 'node:path' import { env } from 'node:process' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../../_testHelpers/index.js' // "Could not find 'Docker', skipping 'Docker' tests." diff --git a/tests/integration/docker/python/python3.6/dockerPython3.6.test.js b/tests/integration/docker/python/python3.6/dockerPython3.6.test.js index 088b5598d..96587460f 100644 --- a/tests/integration/docker/python/python3.6/dockerPython3.6.test.js +++ b/tests/integration/docker/python/python3.6/dockerPython3.6.test.js @@ -1,7 +1,6 @@ import assert from 'node:assert' import { resolve } from 'node:path' import { env } from 'node:process' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../../../_testHelpers/index.js' // "Could not find 'Docker', skipping 'Docker' tests." diff --git a/tests/integration/docker/python/python3.7/dockerPython3.7.test.js b/tests/integration/docker/python/python3.7/dockerPython3.7.test.js index 11a72ef66..8c9b8142b 100644 --- a/tests/integration/docker/python/python3.7/dockerPython3.7.test.js +++ b/tests/integration/docker/python/python3.7/dockerPython3.7.test.js @@ -1,7 +1,6 @@ import assert from 'node:assert' import { resolve } from 'node:path' import { env } from 'node:process' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../../../_testHelpers/index.js' // "Could not find 'Docker', skipping 'Docker' tests." diff --git a/tests/integration/docker/python/python3.8/dockerPython3.8.test.js b/tests/integration/docker/python/python3.8/dockerPython3.8.test.js index c45086240..34a23b203 100644 --- a/tests/integration/docker/python/python3.8/dockerPython3.8.test.js +++ b/tests/integration/docker/python/python3.8/dockerPython3.8.test.js @@ -1,7 +1,6 @@ import assert from 'node:assert' import { resolve } from 'node:path' import { env } from 'node:process' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../../../_testHelpers/index.js' // "Could not find 'Docker', skipping 'Docker' tests." diff --git a/tests/integration/docker/ruby/ruby2.7/dockerRuby2.7.test.js b/tests/integration/docker/ruby/ruby2.7/dockerRuby2.7.test.js index 71e9e6036..eaffdaf15 100644 --- a/tests/integration/docker/ruby/ruby2.7/dockerRuby2.7.test.js +++ b/tests/integration/docker/ruby/ruby2.7/dockerRuby2.7.test.js @@ -1,7 +1,6 @@ import assert from 'node:assert' import { resolve } from 'node:path' import { env } from 'node:process' -import fetch from 'node-fetch' import semver from 'semver' import { joinUrl, setup, teardown } from '../../../_testHelpers/index.js' diff --git a/tests/integration/go/go1.x/go1.x.test.js b/tests/integration/go/go1.x/go1.x.test.js index d3b0c3c6f..de835809a 100644 --- a/tests/integration/go/go1.x/go1.x.test.js +++ b/tests/integration/go/go1.x/go1.x.test.js @@ -2,7 +2,6 @@ import assert from 'node:assert' import { platform } from 'node:os' import { resolve } from 'node:path' import { env } from 'node:process' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../../_testHelpers/index.js' const _describe = diff --git a/tests/integration/groovy/groovy.test.js b/tests/integration/groovy/groovy.test.js index 40c9d6145..17631407e 100644 --- a/tests/integration/groovy/groovy.test.js +++ b/tests/integration/groovy/groovy.test.js @@ -1,7 +1,6 @@ import assert from 'node:assert' import { resolve } from 'node:path' import { env } from 'node:process' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../_testHelpers/index.js' // Could not find 'Java', skipping 'Java' tests. diff --git a/tests/integration/handler/handlerPayload.test.js b/tests/integration/handler/handlerPayload.test.js index 1439e0a08..cad46bc01 100644 --- a/tests/integration/handler/handlerPayload.test.js +++ b/tests/integration/handler/handlerPayload.test.js @@ -2,7 +2,6 @@ import assert from 'node:assert' import { dirname, resolve } from 'node:path' import { env } from 'node:process' import { fileURLToPath } from 'node:url' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../_testHelpers/index.js' const { stringify } = JSON diff --git a/tests/integration/httpApi-cors-default/httpApi-cors-default.test.js b/tests/integration/httpApi-cors-default/httpApi-cors-default.test.js index 73b1def38..f16181626 100644 --- a/tests/integration/httpApi-cors-default/httpApi-cors-default.test.js +++ b/tests/integration/httpApi-cors-default/httpApi-cors-default.test.js @@ -5,7 +5,6 @@ import assert from 'node:assert' import { dirname, resolve } from 'node:path' import { env } from 'node:process' import { fileURLToPath } from 'node:url' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../_testHelpers/index.js' const __dirname = dirname(fileURLToPath(import.meta.url)) diff --git a/tests/integration/httpApi-cors/httpApi-cors.test.js b/tests/integration/httpApi-cors/httpApi-cors.test.js index b99c7e5ed..850dbed0e 100644 --- a/tests/integration/httpApi-cors/httpApi-cors.test.js +++ b/tests/integration/httpApi-cors/httpApi-cors.test.js @@ -5,7 +5,6 @@ import assert from 'node:assert' import { dirname, resolve } from 'node:path' import { env } from 'node:process' import { fileURLToPath } from 'node:url' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../_testHelpers/index.js' const __dirname = dirname(fileURLToPath(import.meta.url)) diff --git a/tests/integration/httpApi-headers/httpApi-headers.test.js b/tests/integration/httpApi-headers/httpApi-headers.test.js index 1d4b36734..c25f24dbb 100644 --- a/tests/integration/httpApi-headers/httpApi-headers.test.js +++ b/tests/integration/httpApi-headers/httpApi-headers.test.js @@ -2,7 +2,6 @@ import assert from 'node:assert' import { dirname, resolve } from 'node:path' import { env } from 'node:process' import { fileURLToPath } from 'node:url' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../_testHelpers/index.js' const __dirname = dirname(fileURLToPath(import.meta.url)) diff --git a/tests/integration/java/java.test.js b/tests/integration/java/java.test.js index 594070d58..c6de3e18d 100644 --- a/tests/integration/java/java.test.js +++ b/tests/integration/java/java.test.js @@ -1,7 +1,6 @@ import assert from 'node:assert' import { resolve } from 'node:path' import { env } from 'node:process' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../_testHelpers/index.js' // Could not find 'Java', skipping 'Java' tests. diff --git a/tests/integration/jwt-authorizer/jwt-authorizer.test.js b/tests/integration/jwt-authorizer/jwt-authorizer.test.js index 23b1b9d23..7b58eec26 100644 --- a/tests/integration/jwt-authorizer/jwt-authorizer.test.js +++ b/tests/integration/jwt-authorizer/jwt-authorizer.test.js @@ -6,7 +6,6 @@ import { randomBytes } from 'node:crypto' import { dirname, resolve } from 'node:path' import { env } from 'node:process' import { fileURLToPath } from 'node:url' -import fetch from 'node-fetch' import jsonwebtoken from 'jsonwebtoken' import { joinUrl, setup, teardown } from '../_testHelpers/index.js' diff --git a/tests/integration/kotlin/kotlin.test.js b/tests/integration/kotlin/kotlin.test.js index db6b63512..815252e64 100644 --- a/tests/integration/kotlin/kotlin.test.js +++ b/tests/integration/kotlin/kotlin.test.js @@ -1,7 +1,6 @@ import assert from 'node:assert' import { resolve } from 'node:path' import { env } from 'node:process' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../_testHelpers/index.js' // Could not find 'Java', skipping 'Java' tests. diff --git a/tests/integration/lambda-integration/lambdaIntegration.test.js b/tests/integration/lambda-integration/lambdaIntegration.test.js index fdb769225..73f2f4186 100644 --- a/tests/integration/lambda-integration/lambdaIntegration.test.js +++ b/tests/integration/lambda-integration/lambdaIntegration.test.js @@ -2,7 +2,6 @@ import assert from 'node:assert' import { dirname, resolve } from 'node:path' import { env } from 'node:process' import { fileURLToPath } from 'node:url' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../_testHelpers/index.js' const { stringify } = JSON diff --git a/tests/integration/lambda-invoke/lambdaInvoke.test.js b/tests/integration/lambda-invoke/lambdaInvoke.test.js index d34285c87..97e2d302d 100644 --- a/tests/integration/lambda-invoke/lambdaInvoke.test.js +++ b/tests/integration/lambda-invoke/lambdaInvoke.test.js @@ -2,7 +2,6 @@ import assert from 'node:assert' import { dirname, resolve } from 'node:path' import { env } from 'node:process' import { fileURLToPath } from 'node:url' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../_testHelpers/index.js' const { isArray } = Array diff --git a/tests/integration/lambda-invoke/lambdaInvokeAsync.test.js b/tests/integration/lambda-invoke/lambdaInvokeAsync.test.js index ac899fc1c..2f2c01244 100644 --- a/tests/integration/lambda-invoke/lambdaInvokeAsync.test.js +++ b/tests/integration/lambda-invoke/lambdaInvokeAsync.test.js @@ -2,7 +2,6 @@ import assert from 'node:assert' import { dirname, resolve } from 'node:path' import { env } from 'node:process' import { fileURLToPath } from 'node:url' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../_testHelpers/index.js' const __dirname = dirname(fileURLToPath(import.meta.url)) diff --git a/tests/integration/override-authorizer/override-authorizer.test.js b/tests/integration/override-authorizer/override-authorizer.test.js index 458eb9cfa..057d86053 100644 --- a/tests/integration/override-authorizer/override-authorizer.test.js +++ b/tests/integration/override-authorizer/override-authorizer.test.js @@ -2,7 +2,6 @@ import assert from 'node:assert' import { dirname, resolve } from 'node:path' import { env } from 'node:process' import { fileURLToPath } from 'node:url' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../_testHelpers/index.js' const { stringify } = JSON diff --git a/tests/integration/python/python-big-json/python-big-json.test.js b/tests/integration/python/python-big-json/python-big-json.test.js index 689279984..4100b5527 100644 --- a/tests/integration/python/python-big-json/python-big-json.test.js +++ b/tests/integration/python/python-big-json/python-big-json.test.js @@ -2,7 +2,6 @@ import assert from 'node:assert' import { platform } from 'node:os' import { resolve } from 'node:path' import { env } from 'node:process' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../../_testHelpers/index.js' // skipping 'Python 3' tests on Windows for now. diff --git a/tests/integration/python/python3/python3.test.js b/tests/integration/python/python3/python3.test.js index 09a219ed7..23c4f3376 100644 --- a/tests/integration/python/python3/python3.test.js +++ b/tests/integration/python/python3/python3.test.js @@ -2,7 +2,6 @@ import assert from 'node:assert' import { platform } from 'node:os' import { resolve } from 'node:path' import { env } from 'node:process' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../../_testHelpers/index.js' // skipping 'Python 3' tests on Windows for now. diff --git a/tests/integration/ruby/ruby.test.js b/tests/integration/ruby/ruby.test.js index 50d01fdb9..39c5081e4 100644 --- a/tests/integration/ruby/ruby.test.js +++ b/tests/integration/ruby/ruby.test.js @@ -1,7 +1,6 @@ import assert from 'node:assert' import { resolve } from 'node:path' import { env } from 'node:process' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../_testHelpers/index.js' // Could not find 'Ruby', skipping 'Ruby' tests. diff --git a/tests/integration/scala/scala.test.js b/tests/integration/scala/scala.test.js index 6d3c27310..f243589f5 100644 --- a/tests/integration/scala/scala.test.js +++ b/tests/integration/scala/scala.test.js @@ -1,7 +1,6 @@ import assert from 'node:assert' import { resolve } from 'node:path' import { env } from 'node:process' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../_testHelpers/index.js' // Could not find 'Java', skipping 'Java' tests. diff --git a/tests/integration/uncategorized/uncategorized.test.js b/tests/integration/uncategorized/uncategorized.test.js index 149140882..7964b5e8d 100644 --- a/tests/integration/uncategorized/uncategorized.test.js +++ b/tests/integration/uncategorized/uncategorized.test.js @@ -2,7 +2,6 @@ import assert from 'node:assert' import { dirname, resolve } from 'node:path' import { env } from 'node:process' import { fileURLToPath } from 'node:url' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../_testHelpers/index.js' const __dirname = dirname(fileURLToPath(import.meta.url)) diff --git a/tests/mochaGlobalSetup.cjs b/tests/mochaGlobalSetup.cjs index c59092c99..f883baab7 100644 --- a/tests/mochaGlobalSetup.cjs +++ b/tests/mochaGlobalSetup.cjs @@ -1,7 +1,15 @@ const { env } = require('node:process') // can be sync or async -exports.mochaGlobalSetup = function mochaGlobalSetup() { +exports.mochaGlobalSetup = async function mochaGlobalSetup() { // env.RUN_TEST_AGAINST_AWS = env.AWS_ENDPOINT != null env.TEST_BASE_URL = env.AWS_ENDPOINT || 'http://localhost:3000' + + // install global fetch + // TODO remove `node-fetch` module and use global built-in with node.js v18+ support + if (globalThis.fetch === undefined) { + const { default: fetch, Headers } = await import('node-fetch') + globalThis.fetch = fetch + globalThis.Headers = Headers + } } diff --git a/tests/scenario/apollo-server-lambda.test.js b/tests/scenario/apollo-server-lambda.test.js index 987a66ec5..61eb9803a 100644 --- a/tests/scenario/apollo-server-lambda.test.js +++ b/tests/scenario/apollo-server-lambda.test.js @@ -2,7 +2,6 @@ import assert from 'node:assert' import { dirname, resolve } from 'node:path' import { env } from 'node:process' import { fileURLToPath } from 'node:url' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../integration/_testHelpers/index.js' import installNpmModules from '../installNpmModules.js' diff --git a/tests/scenario/docker-in-docker.test.js b/tests/scenario/docker-in-docker.test.js index 8e8f35ff8..b0ac722dd 100644 --- a/tests/scenario/docker-in-docker.test.js +++ b/tests/scenario/docker-in-docker.test.js @@ -2,7 +2,6 @@ import assert from 'node:assert' import { platform } from 'node:os' import { env } from 'node:process' import { execa } from 'execa' -import fetch from 'node-fetch' import { compressArtifact, joinUrl } from '../integration/_testHelpers/index.js' // TODO FIXME docker tests currently failing while using node: protocol diff --git a/tests/scenario/docker-serverless-webpack.test.js b/tests/scenario/docker-serverless-webpack.test.js index 9952e80ad..83877783c 100644 --- a/tests/scenario/docker-serverless-webpack.test.js +++ b/tests/scenario/docker-serverless-webpack.test.js @@ -1,7 +1,6 @@ import assert from 'node:assert' import { resolve } from 'node:path' import { env } from 'node:process' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../integration/_testHelpers/index.js' import installNpmModules from '../installNpmModules.js' diff --git a/tests/scenario/serverless-plugin-typescript.test.js b/tests/scenario/serverless-plugin-typescript.test.js index 6ae59b35f..89ba6a2d9 100644 --- a/tests/scenario/serverless-plugin-typescript.test.js +++ b/tests/scenario/serverless-plugin-typescript.test.js @@ -2,7 +2,6 @@ import assert from 'node:assert' import { dirname, resolve } from 'node:path' import { env } from 'node:process' import { fileURLToPath } from 'node:url' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../integration/_testHelpers/index.js' import installNpmModules from '../installNpmModules.js' diff --git a/tests/scenario/serverless-webpack.test.js b/tests/scenario/serverless-webpack.test.js index e9111e179..db85214da 100644 --- a/tests/scenario/serverless-webpack.test.js +++ b/tests/scenario/serverless-webpack.test.js @@ -2,7 +2,6 @@ import assert from 'node:assert' import { dirname, resolve } from 'node:path' import { env } from 'node:process' import { fileURLToPath } from 'node:url' -import fetch from 'node-fetch' import { joinUrl, setup, teardown } from '../integration/_testHelpers/index.js' import installNpmModules from '../installNpmModules.js'