Skip to content

Commit 7b0629c

Browse files
committed
test(web): batch interim test runner
1 parent 4b9ac43 commit 7b0629c

38 files changed

Lines changed: 152 additions & 349 deletions

packages/scripts/src/testing/run.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const bunRuntime = (globalThis as unknown as { Bun: BunRuntime }).Bun;
2121
const WEB_ROOT = resolve(process.cwd(), "packages/web");
2222
const WEB_SRC = resolve(WEB_ROOT, "src");
2323
const WEB_TEST_FILE_PATTERN = /\.(spec|test)\.[tj]sx?$/;
24+
const DEFAULT_WEB_TEST_BATCH_SIZE = 3;
2425

2526
const TEST_PROJECTS = {
2627
backend: {
@@ -62,6 +63,26 @@ function findWebTestFiles(dir: string): string[] {
6263
.sort();
6364
}
6465

66+
function chunk<T>(items: T[], size: number): T[][] {
67+
const chunks: T[][] = [];
68+
69+
for (let index = 0; index < items.length; index += size) {
70+
chunks.push(items.slice(index, index + size));
71+
}
72+
73+
return chunks;
74+
}
75+
76+
function getWebTestBatchSize() {
77+
const configuredBatchSize = Number(process.env["WEB_TEST_BATCH_SIZE"]);
78+
79+
if (Number.isInteger(configuredBatchSize) && configuredBatchSize > 0) {
80+
return configuredBatchSize;
81+
}
82+
83+
return DEFAULT_WEB_TEST_BATCH_SIZE;
84+
}
85+
6586
function assertBackendConfigFile() {
6687
const configFilePath = resolve(process.cwd(), "compass.yaml");
6788

@@ -91,15 +112,16 @@ function runCommand(cmd: string[], cwd = process.cwd()) {
91112
}
92113
}
93114

94-
function runWebProject() {
95-
for (const testFile of findWebTestFiles(WEB_SRC)) {
96-
runCommand([process.execPath, "test", "--cwd", WEB_ROOT, testFile]);
97-
}
98-
}
99-
100115
function runProject(projectName: keyof typeof TEST_PROJECTS) {
101116
if (projectName === "web") {
102-
runWebProject();
117+
const testFileBatches = chunk(
118+
findWebTestFiles(WEB_SRC),
119+
getWebTestBatchSize(),
120+
);
121+
122+
for (const testFiles of testFileBatches) {
123+
runCommand(["bun", "test", "--cwd", WEB_ROOT, ...testFiles]);
124+
}
103125
return;
104126
}
105127

packages/web/src/__tests__/web.preload.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,20 @@ mockNodeModules();
274274
const sessionModule = await import("supertokens-web-js/recipe/session");
275275
const { cleanup } = await import("@testing-library/react");
276276

277+
function resetDocument() {
278+
document.body.innerHTML = "";
279+
document.body.removeAttribute("style");
280+
document.body.removeAttribute("class");
281+
document.body.removeAttribute("data-app-locked");
282+
document.documentElement.removeAttribute("style");
283+
}
284+
285+
function resetBrowserState() {
286+
dom.reconfigure({ url: "http://localhost/" });
287+
localStorage.clear();
288+
sessionStorage.clear();
289+
}
290+
277291
beforeEach(() => {
278292
sessionModule.doesSessionExist?.mockResolvedValue(true);
279293
});
@@ -282,6 +296,8 @@ beforeAll(() => server.listen({ onUnhandledRequest: "error" }));
282296
afterEach(async () => {
283297
await Promise.resolve();
284298
cleanup();
299+
resetDocument();
300+
resetBrowserState();
285301
server.resetHandlers();
286302
});
287303
afterAll(() => server.close());

packages/web/src/auth/compass/hooks/useCompleteAuthentication.test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { afterAll, beforeEach, describe, it, mock } from "bun:test";
1+
import { beforeEach, describe, it, mock } from "bun:test";
22

33
// Pre-define mock functions
44
const mockSyncPendingLocalEvents = mock();
@@ -72,7 +72,3 @@ describe("useCompleteAuthentication", () => {
7272
await Promise.resolve(result.current({ email: "test@example.com" }));
7373
});
7474
});
75-
76-
afterAll(() => {
77-
mock.restore();
78-
});

packages/web/src/auth/compass/hooks/useLogout.test.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
import { act, renderHook } from "@testing-library/react";
2-
import {
3-
afterAll,
4-
beforeEach,
5-
describe,
6-
expect,
7-
it,
8-
mock,
9-
spyOn,
10-
} from "bun:test";
2+
import { beforeEach, describe, expect, it, mock, spyOn } from "bun:test";
113

124
const clearAuthenticationState = mock();
135
const setAuthenticated = mock();
@@ -89,7 +81,3 @@ describe("useLogout", () => {
8981
consoleWarn.mockRestore();
9082
});
9183
});
92-
93-
afterAll(() => {
94-
mock.restore();
95-
});

packages/web/src/auth/compass/session/SessionProvider.test.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { act, renderHook, waitFor } from "@testing-library/react";
2+
import { useContext } from "react";
23
import { Subject } from "rxjs";
34
import { authSlice } from "@web/ducks/auth/slices/auth.slice";
45
import { userMetadataSlice } from "@web/ducks/auth/slices/user-metadata.slice";
5-
import { afterAll, beforeEach, describe, expect, it, mock } from "bun:test";
6+
import { beforeEach, describe, expect, it, mock } from "bun:test";
67

78
// Create mocks at module level
89
const refreshUserMetadata = mock().mockResolvedValue(undefined);
@@ -101,9 +102,8 @@ const { session } = require("@web/common/classes/Session") as {
101102
};
102103
};
103104

104-
const { SessionProvider, sessionInit } =
105+
const { SessionContext, SessionProvider, sessionInit } =
105106
require("./SessionProvider") as typeof import("./SessionProvider");
106-
const { useSession } = require("./useSession") as typeof import("./useSession");
107107

108108
describe("SessionProvider sessionInit", () => {
109109
beforeEach(() => {
@@ -170,7 +170,7 @@ describe("SessionProvider sessionInit", () => {
170170
getStream.mockReturnValue({} as EventSource);
171171
doesSessionExist.mockResolvedValue(false);
172172

173-
const { result } = renderHook(() => useSession(), {
173+
const { result } = renderHook(() => useContext(SessionContext), {
174174
wrapper: SessionProvider,
175175
});
176176

@@ -188,7 +188,3 @@ describe("SessionProvider sessionInit", () => {
188188
expect(result.current.authenticated).toBe(true);
189189
});
190190
});
191-
192-
afterAll(() => {
193-
mock.restore();
194-
});

packages/web/src/auth/compass/session/session.util.test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { UNAUTHENTICATED_USER } from "@web/common/constants/auth.constants";
22
import { getUserId } from "./session.util";
3-
import { afterAll, beforeEach, describe, expect, it, mock } from "bun:test";
3+
import { beforeEach, describe, expect, it, mock } from "bun:test";
44

55
const mockDoesSessionExist = mock();
66
const mockGetAccessTokenPayloadSecurely = mock();
@@ -44,7 +44,3 @@ describe("session.util", () => {
4444
});
4545
});
4646
});
47-
48-
afterAll(() => {
49-
mock.restore();
50-
});

packages/web/src/auth/google/util/google.auth.util.test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
isGoogleRevoked,
44
} from "@web/auth/google/state/google.auth.state";
55
import {
6-
afterAll,
76
afterEach,
87
beforeEach,
98
describe,
@@ -192,7 +191,3 @@ describe("google-auth.util", () => {
192191
});
193192
});
194193
});
195-
196-
afterAll(() => {
197-
mock.restore();
198-
});

packages/web/src/common/hooks/useAuthCmdItems.test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { renderHook } from "@testing-library/react";
22
import { act, type MouseEvent } from "react";
3-
import { afterAll, beforeEach, describe, expect, it, mock } from "bun:test";
3+
import { beforeEach, describe, expect, it, mock } from "bun:test";
44

55
const mockOpenModal = mock();
66
const mockUseAuthModal = mock();
@@ -90,7 +90,3 @@ describe("useAuthCmdItems", () => {
9090
expect(mockOpenModal).toHaveBeenNthCalledWith(2, "login");
9191
});
9292
});
93-
94-
afterAll(() => {
95-
mock.restore();
96-
});

packages/web/src/common/hooks/useLogoutCmdItems.test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { renderHook } from "@testing-library/react";
22
import { act, type MouseEvent } from "react";
3-
import { afterAll, beforeEach, describe, expect, it, mock } from "bun:test";
3+
import { beforeEach, describe, expect, it, mock } from "bun:test";
44

55
const mockOpenLogoutConfirmation = mock();
66
const mockUseLogoutConfirmation = mock();
@@ -55,7 +55,3 @@ describe("useLogoutCmdItems", () => {
5555
expect(mockOpenLogoutConfirmation).toHaveBeenCalledTimes(1);
5656
});
5757
});
58-
59-
afterAll(() => {
60-
mock.restore();
61-
});

packages/web/src/common/hooks/useVersionCheck.test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { renderHook, waitFor } from "@testing-library/react";
22
import { act } from "react";
33
import {
4-
afterAll,
54
afterEach,
65
beforeEach,
76
describe,
@@ -289,7 +288,3 @@ describe("useVersionCheck", () => {
289288
});
290289
});
291290
});
292-
293-
afterAll(() => {
294-
mock.restore();
295-
});

0 commit comments

Comments
 (0)