Files
supabase/e2e/studio/features/_global.setup.ts
Gildas Garcia 13bfc4502a chore: e2e general improvements (#43547)
## Problem

e2e tests are still flaky and not as fast as they could be

## Solution

- [x] Reset supabase instance: this makes the database visualiser tests
faster as there are less tables to screenshot
- [x] Improve the single file setup utilities so that they never block
local tests by cleaning them up before starting
- [x] Disable animations while running the tests (less time waiting for
animations to complete
- [x] Add utility functions that help reproducing flaky tests locally
2026-03-09 15:38:06 +01:00

134 lines
3.3 KiB
TypeScript

import { test as setup } from '@playwright/test'
import dotenv from 'dotenv'
import path from 'node:path'
import os from 'node:os'
import fs from 'node:fs/promises'
import { env } from '../env.config.js'
import { setupProjectForTests } from '../scripts/setup-platform-tests.js'
import { loginWithEmail } from '../scripts/login/email.js'
import { loginWithGithubWithRetry } from '../scripts/login/github.js'
/**
* Run any setup tasks for the tests.
* Catch errors and show useful messages.
*/
dotenv.config({
path: path.resolve(import.meta.dirname, '..', '.env.local'),
override: true,
})
const IS_PLATFORM = process.env.IS_PLATFORM
const doAuthentication = env.AUTHENTICATION
setup('Global Setup', async ({ page }) => {
console.log(`\n 🧪 Setting up test environment.
- Studio URL: ${env.STUDIO_URL}
- API URL: ${env.API_URL}
- Auth: ${doAuthentication ? 'enabled' : 'disabled'}
- Is Platform: ${IS_PLATFORM}
`)
/**
* Studio Check
*/
const studioUrl = env.STUDIO_URL
const apiUrl = env.API_URL
await page.goto(studioUrl).catch((err) => {
console.error(
`\n 🚨 Setup Error
Studio is not available at: ${studioUrl}
Please ensure:
1. Studio is running in the expected URL
2. You have proper network access
`
)
throw err
})
console.log(`\n ✅ Studio is running at ${studioUrl}`)
/**
* API Check
*/
await fetch(apiUrl).catch((err) => {
console.error(`\n 🚨 Setup Error
API is not available at: ${apiUrl}
Please ensure:
1. API is running in the expected URL
2. You have proper network access
To start API locally, run:
npm run dev:api`)
throw new Error('API is not available')
})
console.log(`\n ✅ API is running at ${apiUrl}`)
/**
* Setup Project for tests
*/
const projectRef = await setupProjectForTests()
process.env.PROJECT_REF = projectRef
env.PROJECT_REF = projectRef
/**
* Only run authentication if the environment requires it
*/
if (!doAuthentication) {
console.log(`\n 🔑 Skipping authentication for ${env.STUDIO_URL}`)
return
}
const { EMAIL, PASSWORD } = env
if (EMAIL && PASSWORD) {
console.log(`\n 🔑 Authenticating user with email and password`)
try {
await loginWithEmail(page, studioUrl, {
email: EMAIL,
password: PASSWORD,
})
console.log(`\n ✅ Successfully authenticated with email`)
return
} catch (err) {
console.error(`\n 🚨 Authentication failed with email/password`)
throw err
}
}
const { GITHUB_USER, GITHUB_PASS, GITHUB_TOTP } = env
if (GITHUB_USER && GITHUB_PASS && GITHUB_TOTP) {
console.log(`\n 🔑 Authenticating user with GitHub`)
try {
await loginWithGithubWithRetry({
page,
githubTotp: GITHUB_TOTP,
githubUser: GITHUB_USER,
githubPass: GITHUB_PASS,
supaDashboard: studioUrl,
})
console.log(`\n ✅ Successfully authenticated with GitHub`)
return
} catch (err) {
console.error(`\n 🚨 Authentication failed with GitHub`)
throw err
}
}
// Cleanup locks as they may persist between runs especially locally
const locksDirPath = path.join(os.tmpdir(), 'playwright-locks')
try {
await fs.access(locksDirPath);
await fs.rm(locksDirPath, { recursive: true, force: true })
} catch {
// Silently catch, no directory
}
})