1 import {
2 applyEnvVarsToProcess,
3 formatError,
4 } from "../medusa-test-runner-utils/utils"
5
6 describe("applyEnvVarsToProcess", () => {
7 const NEW_KEY = "MEDUSA_TEST_UTILS_NEW_ENV"
8 const EXISTING_KEY = "MEDUSA_TEST_UTILS_EXISTING_ENV"
9
10 afterEach(() => {
11 delete process.env[NEW_KEY]
12 delete process.env[EXISTING_KEY]
13 })
14
15 it("applies the provided env vars to process.env", () => {
16 applyEnvVarsToProcess({ [NEW_KEY]: "value" })
17 expect(process.env[NEW_KEY]).toBe("value")
18 })
19
20 it("restores previously unset keys by deleting them", () => {
21 delete process.env[NEW_KEY]
22
23 const restore = applyEnvVarsToProcess({ [NEW_KEY]: "value" })
24 expect(process.env[NEW_KEY]).toBe("value")
25
26 restore()
27 expect(process.env[NEW_KEY]).toBeUndefined()
28 })
29
30 it("restores previously set keys to their original value", () => {
31 process.env[EXISTING_KEY] = "original"
32
33 const restore = applyEnvVarsToProcess({ [EXISTING_KEY]: "overridden" })
34 expect(process.env[EXISTING_KEY]).toBe("overridden")
35
36 restore()
37 expect(process.env[EXISTING_KEY]).toBe("original")
38 })
39
40 it("is idempotent when restore runs more than once", () => {
41 process.env[EXISTING_KEY] = "original"
42
43 const restore = applyEnvVarsToProcess({ [EXISTING_KEY]: "overridden" })
44
45 restore()
46 process.env[EXISTING_KEY] = "changed-after-restore"
47 restore()
48 expect(process.env[EXISTING_KEY]).toBe("changed-after-restore")
49 })
50 })
51
52 describe("formatError", () => {
53 it("includes the postgres error details", () => {
54 const error = Object.assign(new Error("database does not exist"), {
55 code: "3D000",
56 detail: "some detail",
57 })
58
59 expect(formatError(error)).toBe(
60 "database does not exist\ncode: 3D000\ndetail: some detail"
61 )
62 })
63
64 it("falls back to the error name when there is no message", () => {
65 expect(formatError(new AggregateError([]))).toBe("AggregateError")
66 })
67
68 it("includes aggregated errors", () => {
69 const error = new AggregateError([new Error("first"), new Error("second")])
70
71 expect(formatError(error)).toBe("AggregateError\nfirst\nsecond")
72 })
73
74 it("includes the error cause", () => {
75 const error = Object.assign(new Error("outer"), {
76 cause: new Error("inner"),
77 })
78
79 expect(formatError(error)).toBe("outer\ncaused by: inner")
80 })
81 })