MCPcopy
hub / github.com/dherault/serverless-offline / setup

Function setup

tests/_testHelpers/setupTeardown.js:11–88  ·  view source on GitHub ↗
(options)

Source from the content-addressed store, hash-verified

9const shouldPrintOfflineOutput = env.PRINT_OFFLINE_OUTPUT
10
11export async function setup(options) {
12 // SERVERLESS_ACCESS_KEY is validated in mochaHooks.cjs before tests run
13 const serverlessAccessKey = env.SERVERLESS_ACCESS_KEY
14
15 const { args = [], env: optionsEnv, servicePath, stdoutData } = options
16 const binary = getBinary()
17 if (!binary.exists()) {
18 await binary.install()
19 if (platform() === "win32") {
20 try {
21 await execa(binary.binaryPath, ["offline", "start", ...args], {
22 cwd: servicePath,
23 env: {
24 SERVERLESS_ACCESS_KEY: serverlessAccessKey,
25 },
26 })
27 } catch {
28 // For some reason it fails on windows with the mock if we don't run it previously without the mock
29 }
30 }
31 }
32
33 serverlessProcess = execa(binary.binaryPath, ["offline", "start", ...args], {
34 cwd: servicePath,
35 env: {
36 ...optionsEnv,
37 SERVERLESS_ACCESS_KEY: serverlessAccessKey,
38 },
39 })
40
41 if (stdoutData) {
42 serverlessProcess.stderr.on("data", stdoutData)
43 serverlessProcess.stdout.on("data", stdoutData)
44 }
45
46 await new Promise((res, reject) => {
47 let stdData = ""
48
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
57 serverlessProcess.on("close", (code) => {
58 if (code) {
59 console.error(`Output: ${stdData}`)
60 reject(new Error("serverless offline crashed"))
61 } else {
62 reject(new Error("serverless offline ended prematurely"))
63 }
64 })
65
66 serverlessProcess.stderr.on("data", (data) => {
67 if (shouldPrintOfflineOutput) process._rawDebug(String(data))
68 stdData += data

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…