Skip to content

Commit

Permalink
Use truly unique Playwright test IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
myieye committed May 16, 2024
1 parent cc97afe commit 77c0a76
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions frontend/tests/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type CreateProjectResponse = {data: {createProject: {createProjectResponse: {id:

type Fixtures = {
contextFactory: (options: BrowserContextOptions) => Promise<BrowserContext>,
uniqueTestId: string,
tempUser: TempUser,
tempProject: TempProject,
tempDir: string,
Expand Down Expand Up @@ -57,6 +58,14 @@ export const test = base.extend<Fixtures>({
addUnexpectedResponseListener(context);
await use(context);
},
// eslint-disable-next-line no-empty-pattern
uniqueTestId: async ({ }, use, testInfo) => {
// testInfo.testId is only guarunteed to be unique within a session (https://playwright.dev/docs/api/class-testcase#test-case-id)
// i.e. it's not unique enough if a test fails to cleanup. We've had that problem.
const shortId = randomUUID().split('-')[0];
const testId = `${testInfo.testId}-${shortId}`;
await use(testId);
},
tempUser: async ({ browser, page }, use, testInfo) => {
const mailinatorId = randomUUID();
const email = `${mailinatorId}@mailinator.com`;
Expand All @@ -76,13 +85,13 @@ export const test = base.extend<Fixtures>({
await deleteUser(context.request, tempUser.id);
await context.close();
},
tempProject: async ({ page }, use, testInfo) => {
tempProject: async ({ page, uniqueTestId }, use, testInfo) => {
const titleForCode =
testInfo.title
.replaceAll(' ','-')
.replaceAll(/[^a-z-]/g,'');
const code = `test-${titleForCode}-${testInfo.testId}`;
const name = `Temporary project for ${testInfo.title} unit test ${testInfo.testId}`;
.replaceAll(/[^a-z-]/g, '');
const code = `test-${titleForCode}-${uniqueTestId}`;
const name = `Temporary project for ${testInfo.title} unit test ${uniqueTestId}`;
const loginData = {
emailOrUsername: 'admin',
password: testEnv.defaultPassword,
Expand Down Expand Up @@ -120,8 +129,8 @@ export const test = base.extend<Fixtures>({
expect(deleteResponse.ok()).toBeTruthy();
},
// eslint-disable-next-line no-empty-pattern
tempDir: async ({}, use, testInfo) => {
const dirname = await mkdtemp(join(tmpdir(), `e2etmp-${testInfo.testId}-`));
tempDir: async ({ uniqueTestId }, use) => {
const dirname = await mkdtemp(join(tmpdir(), `e2etmp-${uniqueTestId}-`));
await use(dirname);
try {
await rm(dirname, { recursive: true, force: true });
Expand Down

0 comments on commit 77c0a76

Please sign in to comment.