Skip to content

Commit 6c3c1f2

Browse files
committed
refactor(tests): enhance project update logic and improve test reliability
- Added a condition to skip environment config overrides for metadata-only project updates in `createOrUpdateProjectWithLegacyConfig`. - Replaced fixed wait times in internal metrics tests with a polling mechanism for better reliability in asynchronous data verification. - Cleaned up the `failed-emails-digest` test by improving the structure and ensuring consistent snapshot expectations. - Removed outdated test for non-existent config files in the local emulator project endpoint, as it is now handled by a different test case.
1 parent 41dbef8 commit 6c3c1f2

4 files changed

Lines changed: 17 additions & 32 deletions

File tree

apps/backend/src/lib/projects.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,13 @@ export async function createOrUpdateProjectWithLegacyConfig(
314314
configOverrideOverride['apps.installed.authentication.enabled'] ??= true;
315315
configOverrideOverride['apps.installed.emails.enabled'] ??= true;
316316
}
317+
if (options.type === "create" || Object.keys(configOverrideOverride).length > 0) {
317318
await overrideEnvironmentConfigOverride({
318319
projectId: projectId,
319320
branchId: branchId,
320321
environmentConfigOverrideOverride: configOverrideOverride,
321322
});
323+
}
322324
const result = await getProject(projectId);
323325
if (!result) {
324326
throw new StackAssertionError("Project not found after creation/update", { projectId });

apps/e2e/tests/backend/endpoints/api/v1/internal-metrics.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,7 @@ it("should return metrics data with users", async ({ expect }) => {
173173
backendContext.set({ mailbox: mailboxes[2], ipData: { country: "CH", ipAddress: "127.0.0.1", city: "Zurich", region: "ZH", latitude: 47.3769, longitude: 8.5417, tzIdentifier: "Europe/Zurich" } });
174174
await Auth.Otp.signIn();
175175

176-
await wait(3000); // the event log is async, so let's give it some time to be written to the DB
177-
178-
const response = await niceBackendFetch("/api/v1/internal/metrics", { accessType: 'admin' });
176+
const response = await waitForMetricsToIncludeUsersByCountry({ countryCode: "CH", expectedCount: 1 });
179177
expect(response).toMatchSnapshot(`metrics_result_with_users`);
180178

181179
await ensureAnonymousUsersAreStillExcluded(response);

apps/e2e/tests/backend/endpoints/api/v1/internal/failed-emails-digest.test.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,26 +104,26 @@ describe("with valid credentials", () => {
104104

105105
await wait(10_000);
106106

107-
const response = await niceBackendFetch("/api/v1/internal/failed-emails-digest", {
108-
method: "POST",
109-
headers: { "Authorization": "Bearer mock_cron_secret" },
110-
query: {
111-
dry_run: `${isDryRun}`,
112-
},
113-
});
107+
const response = await niceBackendFetch("/api/v1/internal/failed-emails-digest", {
108+
method: "POST",
109+
headers: { "Authorization": "Bearer mock_cron_secret" },
110+
query: {
111+
dry_run: `${isDryRun}`,
112+
},
113+
});
114114
expect(response.status).toBe(200);
115115

116116
const failedEmailsByTenancy = response.body.failed_emails_by_tenancy;
117-
const mockProjectFailedEmails = failedEmailsByTenancy.filter(
117+
const mockProjectFailedEmails = failedEmailsByTenancy.filter(
118118
(batch: any) => batch.tenant_owner_emails.includes(backendContext.value.mailbox.emailAddress)
119119
).map((batch: any) => ({
120120
...batch,
121121
emails: [...batch.emails].sort((a, b) => stringCompare(a.subject, b.subject)),
122122
}));
123123

124-
if (process.env.STACK_TEST_SOURCE_OF_TRUTH === "true") {
124+
if (process.env.STACK_TEST_SOURCE_OF_TRUTH === "true") {
125125
expect(mockProjectFailedEmails).toMatchInlineSnapshot(`[]`);
126-
} else {
126+
} else {
127127
expect(mockProjectFailedEmails).toMatchInlineSnapshot(`
128128
[
129129
{
@@ -147,11 +147,11 @@ describe("with valid credentials", () => {
147147
]
148148
`);
149149
expect(mockProjectFailedEmails[0].project_id).toBe(projectId);
150-
}
150+
}
151151

152-
return {
153-
projectOwnerMailbox,
154-
};
152+
return {
153+
projectOwnerMailbox,
154+
};
155155
}
156156

157157
it("should return 200 and process dry run request", async ({ expect }) => {

apps/e2e/tests/backend/endpoints/api/v1/internal/local-emulator-project.test.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,6 @@ describe("local emulator project endpoint", () => {
5353
}
5454
});
5555

56-
it.runIf(isLocalEmulator)("rejects non-existent config files", async ({ expect }) => {
57-
const nonExistentPath = `/tmp/${randomUUID()}/stack.config.ts`;
58-
59-
const response = await niceBackendFetch(LOCAL_EMULATOR_PROJECT_ENDPOINT, {
60-
accessType: "admin",
61-
method: "POST",
62-
body: {
63-
absolute_file_path: nonExistentPath,
64-
},
65-
});
66-
67-
expect(response.status).toBe(400);
68-
expect(response.body).toContain("Config file not found");
69-
});
70-
7156
it.runIf(isLocalEmulator)("writes default config for empty files", async ({ expect }) => {
7257
const filePath = `/tmp/${randomUUID()}/stack.config.ts`;
7358
await fs.mkdir(path.dirname(filePath), { recursive: true });

0 commit comments

Comments
 (0)