Skip to content

Commit acdf34a

Browse files
Vitest stub env refactor (#945)
1 parent ea80400 commit acdf34a

File tree

5 files changed

+22
-44
lines changed

5 files changed

+22
-44
lines changed

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

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

1616
afterEach(() => {
17-
delete process.env.DOCKER_AUTH_CONFIG;
1817
vi.resetModules();
1918
});
2019

2120
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",
21+
vi.stubEnv(
22+
"DOCKER_AUTH_CONFIG",
23+
JSON.stringify({
24+
auths: {
25+
"https://registry.example.com": {
26+
27+
username: "user",
28+
password: "pass",
29+
},
2830
},
29-
},
30-
});
31+
})
32+
);
3133
const { getAuthConfig } = await import("./get-auth-config");
3234
expect(await getAuthConfig("https://registry.example.com")).toEqual({
3335
username: "user",

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

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,12 @@ describe("ContainerImage", () => {
5757
it.each(["custom.com/registry", "custom.com/registry/"])(
5858
"should substitute no registry with the one provided via TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX when provided registry is %s",
5959
(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-
}
60+
vi.stubEnv("TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX", customRegistry);
61+
const imageName = new ImageName(undefined, "image", "tag");
62+
expect(imageName.registry).toBe("custom.com");
63+
expect(imageName.image).toBe("registry/image");
64+
expect(imageName.tag).toBe("tag");
65+
expect(imageName.string).toBe("custom.com/registry/image:tag");
7566
}
7667
);
7768
});
@@ -176,18 +167,8 @@ describe("ContainerImage", () => {
176167
])(
177168
"fromString with TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX set to $customRegistry",
178169
({ customRegistry, expectedRegistry, expectedImagePrefix }) => {
179-
let oldEnvValue: string | undefined;
180170
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-
}
171+
vi.stubEnv("TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX", customRegistry);
191172
});
192173

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

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ import { checkContainerIsHealthy } from "../utils/test-helper";
33
import { GenericContainer } from "./generic-container";
44

55
describe("GenericContainer reuse", { timeout: 180_000 }, () => {
6-
afterEach(() => {
7-
process.env.TESTCONTAINERS_REUSE_ENABLE = undefined;
8-
});
9-
106
it("should not reuse the container by default", async () => {
117
const name = `there_can_only_be_one_${randomUuid()}`;
128
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
@@ -64,7 +60,7 @@ describe("GenericContainer reuse", { timeout: 180_000 }, () => {
6460
});
6561

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

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

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

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ describe("Reaper", { timeout: 120_000 }, () => {
77

88
beforeEach(async () => {
99
vi.resetModules();
10-
vitest.unstubAllEnvs();
11-
1210
client = await getContainerRuntimeClient();
1311
});
1412

@@ -23,7 +21,7 @@ describe("Reaper", { timeout: 120_000 }, () => {
2321
});
2422

2523
it("should propagate TESTCONTAINERS_RYUK_VERBOSE into Reaper container", async () => {
26-
vitest.stubEnv("TESTCONTAINERS_RYUK_VERBOSE", "true");
24+
vi.stubEnv("TESTCONTAINERS_RYUK_VERBOSE", "true");
2725

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

vitest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export default defineConfig({
77
silent: "passed-only",
88
mockReset: true,
99
restoreMocks: true,
10+
unstubEnvs: true,
1011
alias: {
1112
testcontainers: path.resolve(__dirname, "packages/testcontainers/src"),
1213
},

0 commit comments

Comments
 (0)