Skip to content

Commit 89eee41

Browse files
committed
Switch to dependencies for global setup/teardown
This is the recommended approach by Playwright [1], and specifically is useful for me now because it makes it easier to use a non-headless version of the setup for use in debugging. [1] https://playwright.dev/docs/test-global-setup-teardown
1 parent 10d8892 commit 89eee41

File tree

3 files changed

+40
-35
lines changed

3 files changed

+40
-35
lines changed

functional-tests/playwright.config.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const locations = [
4747
];
4848

4949
// Devices
50-
const baseDevices = [
50+
const baseDevices: Array<{ name: string; use: (typeof devices)[string] }> = [
5151
/* Test against desktop browsers */
5252
{
5353
name: "chromium",
@@ -120,10 +120,6 @@ const projects: Project[] = locations.flatMap((geo) =>
120120

121121
export default defineConfig({
122122
testDir: "./tests",
123-
/* Global setup */
124-
globalSetup: "./global-setup.ts",
125-
/* Global teardown */
126-
globalTeardown: "./global-teardown.ts",
127123
/* Maximum time one test can run for. */
128124
timeout: 60_000,
129125
/* Max time in milliseconds the whole test suite can run to prevent CI from hanging. */
@@ -161,7 +157,21 @@ export default defineConfig({
161157
trace: "on-first-retry",
162158
},
163159
/* Configure projects for major browsers */
164-
projects,
160+
projects: [
161+
{
162+
name: "global-setup",
163+
testMatch: /global\-setup/,
164+
teardown: "global-teardown",
165+
},
166+
...projects.map((project) => ({
167+
...project,
168+
dependencies: ["global-setup"],
169+
})),
170+
{
171+
name: "global-teardown",
172+
testMatch: /global\-teardown/,
173+
},
174+
],
165175
/* Folder for test artifacts such as screenshots, videos, traces, etc. */
166176
outputDir: "test-results",
167177
// Run your local dev server before starting the tests -- only on local environment
Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
import { chromium, request } from "@playwright/test";
5+
import { test as setup, request, Browser } from "@playwright/test";
66
import fs from "fs";
77
import path from "path";
88
import { fileURLToPath } from "url";
99
import {
1010
type E2E_TEST_ENV_VALUE,
1111
getBaseTestEnvUrl,
12-
} from "./utils/environment";
13-
import { locations } from "./playwright.config";
14-
import { goToFxA, signUpUser } from "./utils/fxa";
12+
} from "../utils/environment";
13+
import { goToFxA, signUpUser } from "../utils/fxa";
14+
import { locations } from "../playwright.config";
1515

1616
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1717

@@ -32,14 +32,13 @@ async function setupFeatureFlags() {
3232
fs.writeFileSync(
3333
path.resolve(
3434
__dirname,
35-
"./functional-test-cache/enabled-feature-flags.json",
35+
"../functional-test-cache/enabled-feature-flags.json",
3636
),
3737
JSON.stringify({ data: (await response.json()) ?? [] }),
3838
);
3939
}
4040

41-
async function setupUserAccounts() {
42-
const browser = await chromium.launch();
41+
async function setupUserAccount(browser: Browser) {
4342
const emails: Record<string, string> = {};
4443

4544
for (const location of locations) {
@@ -80,22 +79,22 @@ async function setupUserAccounts() {
8079
await context.close();
8180
}
8281

83-
await browser.close();
84-
8582
// Store test user emails
8683
fs.writeFileSync(
87-
path.resolve(__dirname, "./functional-test-cache/user-emails.json"),
84+
path.resolve(__dirname, "../functional-test-cache/user-emails.json"),
8885
JSON.stringify(emails),
8986
);
9087
}
9188

92-
const globalSetup = async () => {
89+
setup("Set up feature flags and user accounts", async ({ browser }) => {
90+
// Creating an account involves waiting for an email to arrive,
91+
// before being able to insert the confirmation code. That takes
92+
// longer than usual.
93+
setup.slow();
9394
// Ensure storage directory exists
94-
const dir = path.resolve(__dirname, "./functional-test-cache");
95+
const dir = path.resolve(__dirname, "../functional-test-cache");
9596
fs.mkdirSync(dir, { recursive: true });
9697

9798
await setupFeatureFlags();
98-
await setupUserAccounts();
99-
};
100-
101-
export default globalSetup;
99+
await setupUserAccount(browser);
100+
});
Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
import { chromium } from "@playwright/test";
5+
import { Browser } from "@playwright/test";
66
import fs from "fs";
77
import path from "path";
88
import { fileURLToPath } from "url";
9-
import { getTestUserEmails, getTestUserSessionFilePath } from "./utils/user";
10-
import { getBaseTestEnvUrl } from "./utils/environment";
9+
import { test as teardown } from "../fixtures/authenticatedTest";
10+
import { getTestUserEmails, getTestUserSessionFilePath } from "../utils/user";
11+
import { getBaseTestEnvUrl } from "../utils/environment";
1112

1213
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1314

1415
function removeTestStorage() {
15-
const dir = path.resolve(__dirname, "./functional-test-cache");
16+
const dir = path.resolve(__dirname, "../functional-test-cache");
1617

1718
if (fs.existsSync(dir)) {
1819
fs.rmSync(dir, { force: true, recursive: true });
1920
console.info("Deleted cache directory after running tests");
2021
}
2122
}
2223

23-
async function deleteTestUserAccounts() {
24-
const browser = await chromium.launch();
24+
async function deleteTestUserAccounts(browser: Browser) {
2525
const userEmails = getTestUserEmails();
2626

2727
for (const userCountryCode in userEmails) {
@@ -42,13 +42,9 @@ async function deleteTestUserAccounts() {
4242

4343
await context.close();
4444
}
45-
46-
await browser.close();
4745
}
4846

49-
const globalTeardown = async () => {
50-
await deleteTestUserAccounts();
47+
teardown("Delete test user accounts", async ({ browser }) => {
48+
await deleteTestUserAccounts(browser);
5149
removeTestStorage();
52-
};
53-
54-
export default globalTeardown;
50+
});

0 commit comments

Comments
 (0)