|
1
|
const path = require("path") |
|
2
|
const { spawn } = require("child_process") |
|
3
|
const { setPort, useExpressServer } = require("./use-api") |
|
4
|
const { setContainer } = require("./use-container") |
|
5
|
|
|
6
|
module.exports = async ({ cwd, redisUrl, uploadDir, verbose, env }) => { |
|
7
|
const serverPath = path.join(__dirname, "test-server.js") |
|
8
|
|
|
9
|
// in order to prevent conflicts in redis, use a different db for each worker |
|
10
|
// same fix as for databases (works with up to 15) |
|
11
|
// redis dbs are 0-indexed and jest worker ids are indexed from 1 |
|
12
|
const workerId = parseInt(process.env.JEST_WORKER_ID || "1") |
|
13
|
const redisUrlWithDatabase = `${redisUrl}/${workerId - 1}` |
|
14
|
|
|
15
|
verbose = verbose ?? false |
|
16
|
|
|
17
|
return await new Promise((resolve, reject) => { |
|
18
|
const medusaProcess = spawn("node", [path.resolve(serverPath)], { |
|
19
|
cwd, |
|
20
|
env: { |
|
21
|
...process.env, |
|
22
|
NODE_ENV: "development", |
|
23
|
JWT_SECRET: "test", |
|
24
|
COOKIE_SECRET: "test", |
|
25
|
REDIS_URL: redisUrl ? redisUrlWithDatabase : undefined, // If provided, will use a real instance, otherwise a fake instance |
|
26
|
UPLOAD_DIR: uploadDir, // If provided, will be used for the fake local file service |
|
27
|
...env, |
|
28
|
}, |
|
29
|
stdio: verbose |
|
30
|
? ["inherit", "inherit", "inherit", "ipc"] |
|
31
|
: ["ignore", "ignore", "ignore", "ipc"], |
|
32
|
}) |
|
33
|
|
|
34
|
medusaProcess.on("error", (err) => { |
|
35
|
console.log(err) |
|
36
|
reject(err) |
|
37
|
process.exit() |
|
38
|
}) |
|
39
|
|
|
40
|
medusaProcess.on("uncaughtException", (err) => { |
|
41
|
console.log(err) |
|
42
|
reject(err) |
|
43
|
medusaProcess.kill() |
|
44
|
}) |
|
45
|
|
|
46
|
medusaProcess.on("message", (port) => { |
|
47
|
setPort(port) |
|
48
|
resolve(medusaProcess) |
|
49
|
}) |
|
50
|
|
|
51
|
medusaProcess.on("exit", () => { |
|
52
|
const expressServer = useExpressServer() |
|
53
|
|
|
54
|
setContainer(null) |
|
55
|
|
|
56
|
if (expressServer) { |
|
57
|
expressServer.close() |
|
58
|
} |
|
59
|
}) |
|
60
|
}) |
|
61
|
} |