-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Playwright refinements. (#315)
* chore: Reset db before all e2e tests. * refactor: Use Playwright's baseURL setting instead of explicitly providing a full URL on all navigations. * refactor: Don't use storageState to log users in. Instead create the user and directly set session cookies when needed. * refactor: Stop using zod to verify config.
- Loading branch information
1 parent
7b6fd6f
commit 9ea82a2
Showing
12 changed files
with
248 additions
and
705 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 21 additions & 13 deletions
34
packages/create-bison-app/template/tests/e2e/auth.play.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,39 @@ | ||
// https://playwright.dev/docs/next/auth#multiple-signed-in-roles | ||
import { Page, test, expect } from '@playwright/test'; | ||
import { Role } from '@prisma/client'; | ||
|
||
import { ADMIN, APP_URL, USER } from './constants'; | ||
import { loginAs } from './helpers'; | ||
|
||
import { resetDB } from '@/tests/helpers/db'; | ||
import { disconnect } from '@/lib/prisma'; | ||
|
||
test.beforeEach(async () => resetDB()); | ||
test.afterAll(async () => disconnect()); | ||
|
||
test.describe(() => { | ||
test.use({ storageState: USER.storageState }); | ||
test('Can login as a User', async ({ page }: { page: Page }) => { | ||
const firstName = 'Qwerty'; | ||
await loginAs(page, { roles: [Role.USER], firstName }); | ||
|
||
test('Can Login as a User', async ({ page }: { page: Page }) => { | ||
await page.goto(APP_URL); | ||
await page.waitForURL((url) => url.origin === APP_URL, { waitUntil: 'networkidle' }); | ||
await page.goto('/'); | ||
await page.waitForSelector('internal:attr=[data-testid="welcome-header"]'); | ||
|
||
const welcomeHeader = await page.getByTestId('welcome-header'); | ||
const welcomeMsg = `Welcome, ${USER.firstName}!`; | ||
const welcomeHeader = page.getByTestId('welcome-header'); | ||
const welcomeMsg = `Welcome, ${firstName}!`; | ||
await expect(welcomeHeader).toContainText(welcomeMsg, { ignoreCase: true }); | ||
}); | ||
}); | ||
|
||
test.describe(() => { | ||
test.use({ storageState: ADMIN.storageState }); | ||
test('Can Login as an Admin', async ({ page }: { page: Page }) => { | ||
await page.goto(APP_URL); | ||
await page.waitForURL((url) => url.origin === APP_URL, { waitUntil: 'networkidle' }); | ||
test('Can login as an Admin', async ({ page }: { page: Page }) => { | ||
const firstName = 'Zxcv'; | ||
await loginAs(page, { roles: [Role.ADMIN], firstName }); | ||
|
||
await page.goto('/'); | ||
await page.waitForSelector('internal:attr=[data-testid="welcome-header"]'); | ||
|
||
const welcomeHeader = await page.getByTestId('welcome-header'); | ||
const welcomeMsg = `Welcome, ${ADMIN.firstName}!`; | ||
const welcomeHeader = page.getByTestId('welcome-header'); | ||
const welcomeMsg = `Welcome, ${firstName}!`; | ||
await expect(welcomeHeader).toContainText(welcomeMsg, { ignoreCase: true }); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
76 changes: 6 additions & 70 deletions
76
packages/create-bison-app/template/tests/e2e/global-setup.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,13 @@ | ||
import { chromium, FullConfig } from '@playwright/test'; | ||
import { Prisma, Role } from '@prisma/client'; | ||
import { promisify } from 'util'; | ||
import childProcess from 'child_process'; | ||
|
||
import { UserFactory } from '../factories'; | ||
import { FullConfig } from '@playwright/test'; | ||
|
||
import { ADMIN, APP_URL, LOGIN_URL, USER } from './constants'; | ||
const exec = promisify(childProcess.exec); | ||
|
||
async function globalSetup(_config: FullConfig) { | ||
// There's a case where a config/setup may fail in which case teardown doesn't fire | ||
// Upsert here to avoid manual cleanup when testing locally. | ||
const adminArgs: Prisma.UserCreateInput = { | ||
email: ADMIN.email, | ||
emailVerified: new Date().toISOString(), | ||
roles: [Role.ADMIN], | ||
password: ADMIN.password, | ||
profile: { | ||
create: { | ||
firstName: ADMIN.firstName, | ||
lastName: ADMIN.lastName, | ||
}, | ||
}, | ||
}; | ||
|
||
const _adminUser = await UserFactory.upsert({ | ||
where: { email: ADMIN.email }, | ||
createArgs: adminArgs, | ||
updateArgs: { | ||
...adminArgs, | ||
profile: { update: { firstName: ADMIN.firstName, lastName: ADMIN.lastName } }, | ||
}, | ||
}).catch((e) => console.log({ e })); | ||
|
||
const userArgs: Prisma.UserCreateInput = { | ||
email: USER.email, | ||
emailVerified: new Date().toISOString(), | ||
roles: [Role.USER], | ||
password: USER.password, | ||
accounts: {}, | ||
profile: { | ||
create: { | ||
firstName: USER.firstName, | ||
lastName: USER.lastName, | ||
}, | ||
}, | ||
}; | ||
|
||
const _user = await UserFactory.upsert({ | ||
where: { email: USER.email }, | ||
createArgs: userArgs, | ||
updateArgs: { | ||
...userArgs, | ||
profile: { update: { firstName: USER.firstName, lastName: USER.lastName } }, | ||
}, | ||
}).catch((e) => console.log({ e })); | ||
|
||
const browser = await chromium.launch(); | ||
const adminPage = await browser.newPage(); | ||
await adminPage.goto(LOGIN_URL); | ||
await adminPage.getByTestId('login-email').fill(ADMIN.email); | ||
await adminPage.getByTestId('login-password').fill(ADMIN.password); | ||
await adminPage.getByTestId('login-submit').click(); | ||
await adminPage.waitForNavigation(); | ||
await adminPage.waitForURL((url) => url.origin === APP_URL, { waitUntil: 'networkidle' }); | ||
await adminPage.context().storageState({ path: ADMIN.storageState }); | ||
|
||
const userPage = await browser.newPage(); | ||
await userPage.goto(LOGIN_URL); | ||
await userPage.getByTestId('login-email').fill(USER.email); | ||
await userPage.getByTestId('login-password').fill(USER.password); | ||
await userPage.getByTestId('login-submit').click(); | ||
await userPage.waitForNavigation(); | ||
await userPage.waitForURL((url) => url.origin === APP_URL, { waitUntil: 'networkidle' }); | ||
await userPage.context().storageState({ path: USER.storageState }); | ||
await browser.close(); | ||
// Run the migrations to ensure our schema has the required structure | ||
await exec('yarn prisma migrate deploy', { env: process.env }); | ||
} | ||
|
||
export default globalSetup; |
8 changes: 6 additions & 2 deletions
8
packages/create-bison-app/template/tests/e2e/global-teardown.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
import { FullConfig } from '@playwright/test'; | ||
|
||
import { prisma } from '@/lib/prisma'; | ||
import { getSchema } from '@/tests/helpers/db'; | ||
import { config } from '@/config'; | ||
|
||
async function globalTeardown(_config: FullConfig) { | ||
prisma.$executeRaw`DROP SCHEMA public CASCADE`; | ||
prisma.$executeRaw`CREATE SCHEMA public`; | ||
const schema = getSchema(config.database.url); | ||
|
||
prisma.$executeRaw`DROP SCHEMA ${schema} CASCADE`; | ||
prisma.$executeRaw`CREATE SCHEMA ${schema}`; | ||
} | ||
|
||
export default globalTeardown; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.