Skip to content

Commit 278c039

Browse files
feat(e2e): add environment-aware fixture loading for staging tests
- Create e2e/fixtures/index.ts with auto-detection of staging environment - Update test files to use authFixtures helper instead of hardcoded paths - Skip tests that require fixtures not available on staging: - profile.spec.ts (needs member-edit fixture) - transaction-details.spec.ts (needs data fixtures) - Fix hardcoded localhost URL in transaction-details.spec.ts Amp-Thread-ID: https://ampcode.com/threads/T-019bf342-cc32-75ec-8607-df28efdf96cd Co-authored-by: Amp <amp@ampcode.com>
1 parent 9eb566a commit 278c039

6 files changed

Lines changed: 86 additions & 5 deletions

File tree

e2e/cta.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { test, expect } from './base-test'
2+
import { authFixtures } from './fixtures'
23

34
test.use({
4-
storageState: 'e2e/fixtures/auth/member-no-transaction.local.json',
5+
storageState: authFixtures.memberNoTransaction,
56
})
67

78
test('Call to action has rendered in dashboard page', async ({ page }) => {

e2e/fixtures/index.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Fixture path helpers for E2E tests.
3+
* Automatically selects local or staging fixtures based on PLAYWRIGHT_BASE_URL or BASE_URL.
4+
*
5+
* The staging config sets PLAYWRIGHT_BASE_URL via the use.baseURL config.
6+
* We also check process.argv for --config=staging to detect staging runs.
7+
*/
8+
9+
const isStaging =
10+
process.env.PLAYWRIGHT_BASE_URL?.includes('staging') ??
11+
process.env.BASE_URL?.includes('staging') ??
12+
process.argv.some((arg) => arg.includes('staging')) ??
13+
false
14+
15+
type AuthFixture =
16+
| 'member'
17+
| 'member-edit'
18+
| 'member-no-transaction'
19+
| 'author'
20+
| 'admin'
21+
| 'public'
22+
| 'nobody'
23+
24+
type DataFixture = 'users' | 'transactions'
25+
26+
/**
27+
* Get the auth fixture path for the current environment.
28+
* Falls back to 'member' for staging if the specific fixture doesn't exist.
29+
*/
30+
export function getAuthFixture(name: AuthFixture): string {
31+
if (isStaging) {
32+
const stagingFixtures = ['member', 'author', 'admin']
33+
const fixtureName = stagingFixtures.includes(name) ? name : 'member'
34+
return `e2e/fixtures/auth/staging/${fixtureName}.staging.json`
35+
}
36+
return `e2e/fixtures/auth/${name}.local.json`
37+
}
38+
39+
/**
40+
* Get the data fixture path for the current environment.
41+
*/
42+
export function getDataFixturePath(type: DataFixture, name: string): string {
43+
const suffix = isStaging ? 'staging' : 'local'
44+
return `../../e2e/fixtures/${type}/${name}.${suffix}.json`
45+
}
46+
47+
/**
48+
* Check if running against staging environment.
49+
* Can be used as a boolean or as a callback for test.skip()
50+
*/
51+
export const isStagingEnv = (): boolean => isStaging
52+
53+
/**
54+
* Auth fixture paths for direct use in test.use()
55+
*/
56+
export const authFixtures = {
57+
member: getAuthFixture('member'),
58+
memberEdit: getAuthFixture('member-edit'),
59+
memberNoTransaction: getAuthFixture('member-no-transaction'),
60+
author: getAuthFixture('author'),
61+
admin: getAuthFixture('admin'),
62+
public: isStaging
63+
? 'e2e/fixtures/auth/public.json'
64+
: 'e2e/fixtures/auth/public.json',
65+
nobody: isStaging
66+
? 'e2e/fixtures/auth/nobody.json'
67+
: 'e2e/fixtures/auth/nobody.json',
68+
}

e2e/logout.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { test, expect } from './base-test'
2+
import { authFixtures } from './fixtures'
23

34
test.use({
4-
storageState: 'e2e/fixtures/auth/member.local.json',
5+
storageState: authFixtures.member,
56
})
67

78
test('Logout', async ({ page, isMobile, screen }) => {

e2e/profile.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { test, expect } from './base-test'
2+
import { authFixtures, isStagingEnv } from './fixtures'
23

34
test.use({
4-
storageState: 'e2e/fixtures/auth/member-edit.local.json',
5+
storageState: authFixtures.memberEdit,
56
})
67

8+
// Skip profile tests on staging - requires member-edit fixture with specific user state
9+
test.skip(isStagingEnv, 'Skipping on staging - requires member-edit fixture')
10+
711
const user = {
812
name: 'Zain Fathoni',
913
phoneNumber: '+6512345678',

e2e/transaction-details.spec.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ import { printRupiah } from '../app/utils/format'
33
import { readFixture } from '../app/utils/fixtures'
44
import { stripLeadingPlus } from '../app/utils/misc'
55
import { test, expect } from './base-test'
6+
import { authFixtures, isStagingEnv } from './fixtures'
67

78
test.use({
8-
storageState: 'e2e/fixtures/auth/author.local.json',
9+
storageState: authFixtures.author,
910
})
1011

1112
let memberSubmit: User, submitted: Transaction, rejected: Transaction
1213

14+
// Skip transaction-details tests on staging (no data fixtures available)
15+
test.skip(isStagingEnv, 'Skipping on staging - no data fixtures available')
16+
1317
test.beforeAll(async () => {
1418
memberSubmit = JSON.parse(
1519
await readFixture(`../../e2e/fixtures/users/member-submit.local.json`)
@@ -24,10 +28,11 @@ test.beforeAll(async () => {
2428

2529
test('redirected to TransactionList page when transaction data with id of $transactionId is not exist', async ({
2630
page,
31+
baseURL,
2732
}) => {
2833
await page.goto('/dashboard/transactions/1')
2934

30-
expect(page.url()).toBe('http://localhost:3000/dashboard/transactions')
35+
expect(page.url()).toBe(`${baseURL}/dashboard/transactions`)
3136
})
3237

3338
test('render transaction data if transaction data exists', async ({ page }) => {

playwright-staging-setup.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const REQUIRED_AUTH_FILES = [
2626
* 3. Close the browser - storage state is saved automatically
2727
*/
2828
async function globalSetup() {
29+
// Set BASE_URL for fixture path resolution
30+
process.env.BASE_URL = 'https://staging.kelas.rumahberbagi.com'
2931
if (!fs.existsSync(STAGING_AUTH_DIR)) {
3032
fs.mkdirSync(STAGING_AUTH_DIR, { recursive: true })
3133
console.log(`Created staging auth directory: ${STAGING_AUTH_DIR}`)

0 commit comments

Comments
 (0)