diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 798acef3d..01cf334a5 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -9,7 +9,6 @@ on: env: FORCE_COLOR: 1 PRINT_OFFLINE_OUTPUT: 1 - SERVERLESS_ACCESS_KEY: ${{ secrets.SERVERLESS_ACCESS_KEY }} jobs: build: diff --git a/package-lock.json b/package-lock.json index 6ca066dda..767a7a0d7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,6 +27,7 @@ "jsonschema": "^1.4.1", "jszip": "^3.10.1", "luxon": "^3.5.0", + "nock": "^13.5.6", "node-fetch": "^3.3.2", "node-schedule": "^2.1.1", "p-memoize": "^7.1.1", @@ -7457,8 +7458,7 @@ "node_modules/json-stringify-safe": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz", - "integrity": "sha512-ZClg6AaYvamvYEE82d3Iyd3vSSIjQ+odgjaTzRuO3s7toCdFKczob2i0zCh7JE8kWn17yvAWhUVxvqGwUalsRA==", - "dev": true + "integrity": "sha512-ZClg6AaYvamvYEE82d3Iyd3vSSIjQ+odgjaTzRuO3s7toCdFKczob2i0zCh7JE8kWn17yvAWhUVxvqGwUalsRA==" }, "node_modules/json5": { "version": "2.2.3", @@ -8378,6 +8378,19 @@ "integrity": "sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==", "dev": true }, + "node_modules/nock": { + "version": "13.5.6", + "resolved": "https://registry.npmjs.org/nock/-/nock-13.5.6.tgz", + "integrity": "sha512-o2zOYiCpzRqSzPj0Zt/dQ/DqZeYoaQ7TUonc/xUPjCGl9WeHpNbxgVvOquXYAaJzI0M9BXV3HTzG0p8IUAbBTQ==", + "dependencies": { + "debug": "^4.1.0", + "json-stringify-safe": "^5.0.1", + "propagate": "^2.0.0" + }, + "engines": { + "node": ">= 10.13" + } + }, "node_modules/node-domexception": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz", @@ -9212,6 +9225,14 @@ "node": ">=8" } }, + "node_modules/propagate": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/propagate/-/propagate-2.0.1.tgz", + "integrity": "sha512-vGrhOavPSTz4QVNuBNdcNXePNdNMaO1xj9yBeH1ScQPjk/rhg9sSlCXPhMkFuaNNW/syTvYqsnbIJxMBfRbbag==", + "engines": { + "node": ">= 8" + } + }, "node_modules/proxy-from-env": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", diff --git a/package.json b/package.json index 77ee2df87..81e7df9c7 100644 --- a/package.json +++ b/package.json @@ -94,6 +94,7 @@ "jsonschema": "^1.4.1", "jszip": "^3.10.1", "luxon": "^3.5.0", + "nock": "^13.5.6", "node-fetch": "^3.3.2", "node-schedule": "^2.1.1", "p-memoize": "^7.1.1", diff --git a/tests/_testHelpers/serverlessApiMockSetup.cjs b/tests/_testHelpers/serverlessApiMockSetup.cjs new file mode 100644 index 000000000..8cae76e44 --- /dev/null +++ b/tests/_testHelpers/serverlessApiMockSetup.cjs @@ -0,0 +1,16 @@ +const nock = require("nock") + +nock("https://core.serverless.com/api") + .post("/bff/") + .reply(200, { + data: { + callerIdentity: { + orgId: "your-mocked-org-id", + orgName: "your-mocked-org-name", + userEmail: "your-mocked-email@example.com", + userId: "your-mocked-user-id", + userName: "your-mocked-user-name", + }, + }, + }) +console.log("Serverless API mock setup loaded!") diff --git a/tests/_testHelpers/setupTeardown.js b/tests/_testHelpers/setupTeardown.js index 89c4211f6..e6b3b89b7 100644 --- a/tests/_testHelpers/setupTeardown.js +++ b/tests/_testHelpers/setupTeardown.js @@ -1,20 +1,41 @@ import process, { env } from "node:process" import { execa } from "execa" +import { platform } from "node:os" +import { join } from "desm" import treeKill from "tree-kill" -import { install, getBinary } from "serverless/binary.js" +import { getBinary } from "serverless/binary.js" let serverlessProcess const shouldPrintOfflineOutput = env.PRINT_OFFLINE_OUTPUT export async function setup(options) { - await install() - const binary = getBinary() const { args = [], env: optionsEnv, servicePath, stdoutData } = options + const binary = getBinary() + if (!binary.exists()) { + await binary.install() + if (platform() === "win32") { + try { + await execa(binary.binaryPath, ["offline", "start", ...args], { + cwd: servicePath, + env: { + SERVERLESS_ACCESS_KEY: "MOCK_ACCESS_KEY", + }, + }) + } catch { + // For some reason it fails on windows with the mock if we don't run it previously without the mock + } + } + } + const mockSetupPath = join(import.meta.url, "serverlessApiMockSetup.cjs") serverlessProcess = execa(binary.binaryPath, ["offline", "start", ...args], { cwd: servicePath, - env: optionsEnv, + env: { + ...optionsEnv, + NODE_OPTIONS: `--require ${mockSetupPath}`, + SERVERLESS_ACCESS_KEY: "MOCK_ACCESS_KEY", + }, }) if (stdoutData) { @@ -25,6 +46,14 @@ export async function setup(options) { await new Promise((res, reject) => { let stdData = "" + serverlessProcess.on("uncaughtException", (err) => { + console.error("Uncaught Exception:", err) + }) + + serverlessProcess.on("unhandledRejection", (reason, p) => { + console.error(reason, "Unhandled Rejection at Promise", p) + }) + serverlessProcess.on("close", (code) => { if (code) { console.error(`Output: ${stdData}`)