Skip to content

Commit d3519c2

Browse files
authored
ci: mock serverless api response for tests (#1840)
* 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
1 parent 903340a commit d3519c2

File tree

5 files changed

+73
-7
lines changed

5 files changed

+73
-7
lines changed

.github/workflows/nodejs.yml

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ on:
99
env:
1010
FORCE_COLOR: 1
1111
PRINT_OFFLINE_OUTPUT: 1
12-
SERVERLESS_ACCESS_KEY: ${{ secrets.SERVERLESS_ACCESS_KEY }}
1312

1413
jobs:
1514
build:

package-lock.json

+23-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
"jsonschema": "^1.4.1",
9595
"jszip": "^3.10.1",
9696
"luxon": "^3.5.0",
97+
"nock": "^13.5.6",
9798
"node-fetch": "^3.3.2",
9899
"node-schedule": "^2.1.1",
99100
"p-memoize": "^7.1.1",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const nock = require("nock")
2+
3+
nock("https://core.serverless.com/api")
4+
.post("/bff/")
5+
.reply(200, {
6+
data: {
7+
callerIdentity: {
8+
orgId: "your-mocked-org-id",
9+
orgName: "your-mocked-org-name",
10+
userEmail: "[email protected]",
11+
userId: "your-mocked-user-id",
12+
userName: "your-mocked-user-name",
13+
},
14+
},
15+
})
16+
console.log("Serverless API mock setup loaded!")

tests/_testHelpers/setupTeardown.js

+33-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,41 @@
11
import process, { env } from "node:process"
22
import { execa } from "execa"
3+
import { platform } from "node:os"
4+
import { join } from "desm"
35
import treeKill from "tree-kill"
4-
import { install, getBinary } from "serverless/binary.js"
6+
import { getBinary } from "serverless/binary.js"
57

68
let serverlessProcess
79

810
const shouldPrintOfflineOutput = env.PRINT_OFFLINE_OUTPUT
911

1012
export async function setup(options) {
11-
await install()
12-
const binary = getBinary()
1313
const { args = [], env: optionsEnv, servicePath, stdoutData } = options
14+
const binary = getBinary()
15+
if (!binary.exists()) {
16+
await binary.install()
17+
if (platform() === "win32") {
18+
try {
19+
await execa(binary.binaryPath, ["offline", "start", ...args], {
20+
cwd: servicePath,
21+
env: {
22+
SERVERLESS_ACCESS_KEY: "MOCK_ACCESS_KEY",
23+
},
24+
})
25+
} catch {
26+
// For some reason it fails on windows with the mock if we don't run it previously without the mock
27+
}
28+
}
29+
}
30+
const mockSetupPath = join(import.meta.url, "serverlessApiMockSetup.cjs")
1431

1532
serverlessProcess = execa(binary.binaryPath, ["offline", "start", ...args], {
1633
cwd: servicePath,
17-
env: optionsEnv,
34+
env: {
35+
...optionsEnv,
36+
NODE_OPTIONS: `--require ${mockSetupPath}`,
37+
SERVERLESS_ACCESS_KEY: "MOCK_ACCESS_KEY",
38+
},
1839
})
1940

2041
if (stdoutData) {
@@ -25,6 +46,14 @@ export async function setup(options) {
2546
await new Promise((res, reject) => {
2647
let stdData = ""
2748

49+
serverlessProcess.on("uncaughtException", (err) => {
50+
console.error("Uncaught Exception:", err)
51+
})
52+
53+
serverlessProcess.on("unhandledRejection", (reason, p) => {
54+
console.error(reason, "Unhandled Rejection at Promise", p)
55+
})
56+
2857
serverlessProcess.on("close", (code) => {
2958
if (code) {
3059
console.error(`Output: ${stdData}`)

0 commit comments

Comments
 (0)