Skip to content

Commit 399b579

Browse files
authored
test: improve script to reset tests (#15328)
discussed internally
1 parent c2baef4 commit 399b579

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

‎test/helpers/reset.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,16 @@ export async function resetDB(_payload: Payload, collectionSlugs: string[]) {
4343
drizzle: db.drizzle,
4444
raw: queries,
4545
})
46+
}elseif(
47+
'clearDatabase'in_payload.db&&
48+
typeof(_payload.dbasany).clearDatabase==='function'
49+
){
50+
console.log('[resetDB] using clearDatabase method')
51+
await(_payload.dbasany).clearDatabase()
52+
}else{
53+
// Fallback for other unknown adapters
54+
console.warn(
55+
'[resetDB] No reset method available for this adapter. Database will not be cleared.',
56+
)
4657
}
4758
}

‎test/helpers/snapshot.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,16 @@ export async function createSnapshot(
127127
awaitcreateMongooseSnapshot(mongooseCollections,snapshotKey)
128128
}else{
129129
constdb: PostgresAdapter=_payload.dbasunknownasPostgresAdapter
130-
awaitcreateDrizzleSnapshot(db,snapshotKey)
130+
// Only create snapshot if drizzle is available (Postgres/SQLite adapters)
131+
if(db.drizzle){
132+
awaitcreateDrizzleSnapshot(db,snapshotKey)
133+
}
134+
// For adapters that don't support snapshots (e.g., content-api), we intentionally
135+
// don't set dbSnapshot[snapshotKey] at all. This is important because:
136+
// 1. seedDB() checks if dbSnapshot[snapshotKey] exists (line 78)
137+
// 2. If it exists but is empty {}, seedDB() won't restore BUT also won't create a snapshot
138+
// 3. By not setting it, subsequent test runs will always do reset + seed (no snapshot caching)
139+
// 4. This is correct for remote services like Content API where snapshots aren't possible
131140
}
132141
}
133142

@@ -145,6 +154,12 @@ export async function restoreFromSnapshot(
145154
awaitrestoreFromMongooseSnapshot(mongooseCollections,snapshotKey)
146155
}else{
147156
constdb: PostgresAdapter=_payload.dbasunknownasPostgresAdapter
148-
awaitrestoreFromDrizzleSnapshot(db,snapshotKey)
157+
// Only restore from snapshot if drizzle is available (Postgres/SQLite adapters)
158+
if(db.drizzle){
159+
awaitrestoreFromDrizzleSnapshot(db,snapshotKey)
160+
}else{
161+
// Skip restore for other adapters (e.g., content-api)
162+
// These adapters typically re-seed or handle cleanup differently
163+
}
149164
}
150165
}

0 commit comments

Comments
 (0)