Skip to content

Commit

Permalink
ci: mock serverless api response for tests (#1840)
Browse files Browse the repository at this point in the history
* ci: mock serverless api response for tests

* ci: fix missing __dirname

* ci: fix mock setup

* ci: add more logs

* ci: fix path for windows

* ci: fix windows path

* ci: fix workflow on windows
  • Loading branch information
DorianMazur authored Dec 3, 2024
1 parent 903340a commit d3519c2
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 7 deletions.
1 change: 0 additions & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ on:
env:
FORCE_COLOR: 1
PRINT_OFFLINE_OUTPUT: 1
SERVERLESS_ACCESS_KEY: ${{ secrets.SERVERLESS_ACCESS_KEY }}

jobs:
build:
Expand Down
25 changes: 23 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 16 additions & 0 deletions tests/_testHelpers/serverlessApiMockSetup.cjs
Original file line number Diff line number Diff line change
@@ -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: "[email protected]",
userId: "your-mocked-user-id",
userName: "your-mocked-user-name",
},
},
})
console.log("Serverless API mock setup loaded!")
37 changes: 33 additions & 4 deletions tests/_testHelpers/setupTeardown.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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}`)
Expand Down

0 comments on commit d3519c2

Please sign in to comment.