Skip to content

Commit 0fb5e9a

Browse files
Reactor setting env vars in tests
1 parent 29f2220 commit 0fb5e9a

File tree

4 files changed

+30
-42
lines changed

4 files changed

+30
-42
lines changed

packages/testcontainers/src/container-runtime/auth/get-auth-config.test.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,23 @@ describe("get auth config", () => {
1414
});
1515

1616
afterEach(() => {
17-
delete process.env.DOCKER_AUTH_CONFIG;
17+
vi.unstubAllEnvs();
1818
vi.resetModules();
1919
});
2020

2121
it("should use DOCKER_AUTH_CONFIG environment variable as Docker config", async () => {
22-
process.env.DOCKER_AUTH_CONFIG = JSON.stringify({
23-
auths: {
24-
"https://registry.example.com": {
25-
26-
username: "user",
27-
password: "pass",
22+
vi.stubEnv(
23+
"DOCKER_AUTH_CONFIG",
24+
JSON.stringify({
25+
auths: {
26+
"https://registry.example.com": {
27+
28+
username: "user",
29+
password: "pass",
30+
},
2831
},
29-
},
30-
});
32+
})
33+
);
3134
const { getAuthConfig } = await import("./get-auth-config");
3235
expect(await getAuthConfig("https://registry.example.com")).toEqual({
3336
username: "user",

packages/testcontainers/src/container-runtime/image-name.test.ts

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ describe("ContainerImage", () => {
1010
expect(imageName.equals(new ImageName("anotherRegistry", "image", "tag"))).toBe(false);
1111
});
1212

13+
afterEach(() => {
14+
vi.unstubAllEnvs();
15+
});
16+
1317
describe("string", () => {
1418
it("should work with registry", () => {
1519
const imageName = new ImageName("registry", "image", "tag");
@@ -57,21 +61,12 @@ describe("ContainerImage", () => {
5761
it.each(["custom.com/registry", "custom.com/registry/"])(
5862
"should substitute no registry with the one provided via TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX when provided registry is %s",
5963
(customRegistry: string) => {
60-
const oldEnvValue = process.env.TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX;
61-
try {
62-
process.env.TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX = customRegistry;
63-
const imageName = new ImageName(undefined, "image", "tag");
64-
expect(imageName.registry).toBe("custom.com");
65-
expect(imageName.image).toBe("registry/image");
66-
expect(imageName.tag).toBe("tag");
67-
expect(imageName.string).toBe("custom.com/registry/image:tag");
68-
} finally {
69-
if (oldEnvValue === undefined) {
70-
delete process.env.TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX;
71-
} else {
72-
process.env.TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX = oldEnvValue;
73-
}
74-
}
64+
vi.stubEnv("TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX", customRegistry);
65+
const imageName = new ImageName(undefined, "image", "tag");
66+
expect(imageName.registry).toBe("custom.com");
67+
expect(imageName.image).toBe("registry/image");
68+
expect(imageName.tag).toBe("tag");
69+
expect(imageName.string).toBe("custom.com/registry/image:tag");
7570
}
7671
);
7772
});
@@ -176,18 +171,8 @@ describe("ContainerImage", () => {
176171
])(
177172
"fromString with TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX set to $customRegistry",
178173
({ customRegistry, expectedRegistry, expectedImagePrefix }) => {
179-
let oldEnvValue: string | undefined;
180174
beforeEach(() => {
181-
oldEnvValue = process.env.TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX;
182-
process.env.TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX = customRegistry;
183-
});
184-
185-
afterEach(() => {
186-
if (oldEnvValue === undefined) {
187-
delete process.env.TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX;
188-
} else {
189-
process.env.TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX = oldEnvValue;
190-
}
175+
vi.stubEnv("TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX", customRegistry);
191176
});
192177

193178
it("should work", () => {

packages/testcontainers/src/generic-container/generic-container-reuse.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { GenericContainer } from "./generic-container";
44

55
describe("GenericContainer reuse", { timeout: 180_000 }, () => {
66
afterEach(() => {
7-
process.env.TESTCONTAINERS_REUSE_ENABLE = undefined;
7+
vi.unstubAllEnvs();
88
});
99

1010
it("should not reuse the container by default", async () => {
@@ -64,7 +64,7 @@ describe("GenericContainer reuse", { timeout: 180_000 }, () => {
6464
});
6565

6666
it("should not reuse the container when TESTCONTAINERS_REUSE_ENABLE is set to false", async () => {
67-
process.env.TESTCONTAINERS_REUSE_ENABLE = "false";
67+
vi.stubEnv("TESTCONTAINERS_REUSE_ENABLE", "false");
6868

6969
const name = `there_can_only_be_one_${randomUuid()}`;
7070
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
@@ -90,7 +90,7 @@ describe("GenericContainer reuse", { timeout: 180_000 }, () => {
9090
it.each(["true", undefined])(
9191
"should reuse the container when TESTCONTAINERS_REUSE_ENABLE is set to %s",
9292
async (reuseEnable: string | undefined) => {
93-
process.env.TESTCONTAINERS_REUSE_ENABLE = reuseEnable;
93+
vi.stubEnv("TESTCONTAINERS_REUSE_ENABLE", reuseEnable);
9494

9595
const name = `there_can_only_be_one_${randomUuid()}`;
9696
const container1 = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")

packages/testcontainers/src/reaper/reaper.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ describe("Reaper", { timeout: 120_000 }, () => {
88

99
beforeEach(async () => {
1010
vi.resetModules();
11-
vitest.unstubAllEnvs();
11+
vi.unstubAllEnvs();
1212

1313
client = await getContainerRuntimeClient();
1414
});
1515

1616
it("should create disabled reaper when TESTCONTAINERS_RYUK_DISABLED=true", async () => {
17-
vitest.stubEnv("TESTCONTAINERS_RYUK_DISABLED", "true");
17+
vi.stubEnv("TESTCONTAINERS_RYUK_DISABLED", "true");
1818

1919
vi.spyOn(client.container, "list").mockResolvedValue([]);
2020
const reaper = await getReaper();
@@ -29,7 +29,7 @@ describe("Reaper", { timeout: 120_000 }, () => {
2929

3030
it("should use custom port when TESTCONTAINERS_RYUK_PORT is set", async () => {
3131
const customPort = (await new RandomUniquePortGenerator().generatePort()).toString();
32-
vitest.stubEnv("TESTCONTAINERS_RYUK_PORT", customPort);
32+
vi.stubEnv("TESTCONTAINERS_RYUK_PORT", customPort);
3333

3434
vi.spyOn(client.container, "list").mockResolvedValue([]);
3535
const reaper = await getReaper();
@@ -60,7 +60,7 @@ describe("Reaper", { timeout: 120_000 }, () => {
6060
});
6161

6262
it("should propagate TESTCONTAINERS_RYUK_VERBOSE into Reaper container", async () => {
63-
vitest.stubEnv("TESTCONTAINERS_RYUK_VERBOSE", "true");
63+
vi.stubEnv("TESTCONTAINERS_RYUK_VERBOSE", "true");
6464

6565
vi.spyOn(client.container, "list").mockResolvedValue([]);
6666
const reaper = await getReaper();

0 commit comments

Comments
 (0)