Skip to content
This repository was archived by the owner on Jan 4, 2025. It is now read-only.

Commit 047ddf6

Browse files
authored
Merge pull request #99 from johnnygerard/test
test: improve Playwright test performance
2 parents f218be3 + 07f5905 commit 047ddf6

14 files changed

+63
-20
lines changed

client/e2e/fixtures.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { test as base } from "@playwright/test";
2+
3+
export const test = base.extend({
4+
page: async ({ page }, use) => {
5+
// Disable Angular animations (see `app.config.ts`)
6+
await page.emulateMedia({ reducedMotion: "reduce" });
7+
await use(page);
8+
},
9+
});

client/e2e/global-setup.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ import ms from "ms";
33
import child_process from "node:child_process";
44
import { promisify } from "node:util";
55
import { globalCredentials } from "./global-credentials";
6-
import { registerUser } from "./register-user.function";
6+
import { registerUserViaApi } from "./register-user.function";
77

88
const exec = promisify(child_process.exec);
99

10-
test("Global setup", async ({ page }) => {
10+
test("Global setup", async ({ page, request }) => {
1111
test.setTimeout(ms("20 seconds"));
1212
await exec("docker compose up --detach");
1313

1414
console.log("Waiting for servers to be ready...");
1515
await exec("npm run wait:servers");
1616
console.log("Servers are ready ✔");
1717

18-
await registerUser(page, globalCredentials);
18+
await registerUserViaApi(request, globalCredentials);
1919
console.log("Global setup complete ✔");
2020
});

client/e2e/register-user.function.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { faker } from "@faker-js/faker";
2-
import { expect, Page } from "@playwright/test";
2+
import { APIRequestContext, expect, Page } from "@playwright/test";
33
import { CREATED } from "_server/constants/http-status-code";
44
import { Credentials } from "_server/types/credentials";
55

@@ -33,3 +33,23 @@ export const registerUser = async (
3333
return response.finished();
3434
});
3535
};
36+
37+
/**
38+
* Register a user via the API while verifying the response status.
39+
* @param request - The API request context
40+
* @param credentials - The user's credentials to register with
41+
* @param expectedStatus - The expected response status code
42+
*/
43+
export const registerUserViaApi = async (
44+
request: APIRequestContext,
45+
credentials?: Partial<Credentials>,
46+
expectedStatus = CREATED,
47+
): Promise<void> => {
48+
const data: Credentials = {
49+
username: credentials?.username ?? faker.internet.userName(),
50+
password: credentials?.password ?? faker.internet.password(),
51+
};
52+
53+
const response = await request.post("/api/account", { data });
54+
expect(response.status()).toBe(expectedStatus);
55+
};

client/e2e/tests/account-deletion.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { faker } from "@faker-js/faker";
2-
import { expect, Locator, test } from "@playwright/test";
2+
import { expect, Locator } from "@playwright/test";
33
import { NO_CONTENT } from "_server/constants/http-status-code";
44
import { UserMessage } from "../../src/app/types/user-message.enum";
55
import { getResponsePredicate } from "../extensions";
6+
import { test } from "../fixtures";
67
import { registerUser } from "../register-user.function";
78

89
test.describe("Account deletion", () => {

client/e2e/tests/password-validation.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { faker } from "@faker-js/faker";
2-
import { expect, Locator, test } from "@playwright/test";
2+
import { expect, Locator } from "@playwright/test";
33
import { PASSWORD_MAX_LENGTH } from "_server/constants/password";
4+
import { test } from "../fixtures";
45

56
test.describe("Password validation", () => {
67
let username: string;

client/e2e/tests/password-visibility.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { faker } from "@faker-js/faker";
2-
import { expect, Locator, test } from "@playwright/test";
2+
import { expect, Locator } from "@playwright/test";
3+
import { test } from "../fixtures";
34

45
test.describe("Password visibility", () => {
56
let passwordInput: Locator;

client/e2e/tests/rate-limiter.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { faker } from "@faker-js/faker";
2-
import { test } from "@playwright/test";
32
import { TOO_MANY_REQUESTS } from "_server/constants/http-status-code";
3+
import { test } from "../fixtures";
44

55
// This test should be run separately. To run it:
66
// - Replace `describe.skip` with `describe.only`

client/e2e/tests/session-revocation.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import { expect, test } from "@playwright/test";
1+
import { expect } from "@playwright/test";
22
import { getFakeCredentials } from "_server/test-helpers/faker-extensions";
33
import { Credentials } from "_server/types/credentials";
44
import { UserMessage } from "../../src/app/types/user-message.enum";
5+
import { test } from "../fixtures";
56
import { logInUser } from "../log-in-user.function";
6-
import { registerUser } from "../register-user.function";
7+
import { registerUserViaApi } from "../register-user.function";
78

89
test.describe("Session revocation", () => {
910
let credentials: Credentials;
1011

11-
test.beforeEach(async ({ browser, page }) => {
12-
const context = await browser.newContext();
12+
test.beforeEach(async ({ page, request }) => {
1313
credentials = getFakeCredentials();
1414

15-
await registerUser(await context.newPage(), credentials);
15+
await registerUserViaApi(request, credentials);
1616
await logInUser(page, credentials);
1717
});
1818

client/e2e/tests/user-login.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { expect, test } from "@playwright/test";
1+
import { expect } from "@playwright/test";
22
import { UNAUTHORIZED } from "_server/constants/http-status-code";
3+
import { test } from "../fixtures";
34
import { globalCredentials } from "../global-credentials";
45
import { logInUser } from "../log-in-user.function";
56

client/e2e/tests/user-logout.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { expect, test } from "@playwright/test";
1+
import { expect } from "@playwright/test";
22
import { NO_CONTENT } from "_server/constants/http-status-code";
3+
import { test } from "../fixtures";
34
import { globalCredentials } from "../global-credentials";
45
import { logInUser } from "../log-in-user.function";
56

0 commit comments

Comments
 (0)