@@ -37,11 +37,10 @@ const CLIENT_CONFIG: Partial<ClientConfig> = {
3737} ;
3838
3939// Track all projects created with external DB configs for cleanup
40- type ProjectContext = {
40+ export type ProjectContext = {
4141 projectId : string ,
4242 superSecretAdminKey : string ,
4343} ;
44- const createdProjects : ProjectContext [ ] = [ ] ;
4544
4645/**
4746 * Helper class to manage external test databases
@@ -50,6 +49,7 @@ export class TestDbManager {
5049 private setupClient : Client | null = null ;
5150 private databases : Map < string , Client > = new Map ( ) ;
5251 private databaseNames : Set < string > = new Set ( ) ;
52+ public readonly createdProjects : ProjectContext [ ] = [ ] ;
5353
5454 async init ( ) {
5555 this . setupClient = new Client ( {
@@ -84,7 +84,8 @@ export class TestDbManager {
8484
8585 async cleanup ( ) {
8686 // First, clean up all project configs to stop the sync cron from trying to connect
87- await cleanupAllProjectConfigs ( ) ;
87+ await cleanupProjectConfigs ( this . createdProjects ) ;
88+ this . createdProjects . length = 0 ;
8889
8990 // Close all tracked database clients
9091 const closePromises = Array . from ( this . databases . values ( ) ) . map ( async ( client ) => {
@@ -365,7 +366,11 @@ export async function countUsersInExternalDb(client: Client): Promise<number> {
365366 * Helper to create a project and update its config with external DB settings.
366367 * Tracks the project for cleanup later.
367368 */
368- export async function createProjectWithExternalDb ( externalDatabases : any , projectOptions ?: { display_name ?: string , description ?: string } ) {
369+ export async function createProjectWithExternalDb (
370+ externalDatabases : any ,
371+ projectOptions ?: { display_name ?: string , description ?: string } ,
372+ options ?: { projectTracker ?: ProjectContext [ ] }
373+ ) {
369374 const project = await Project . createAndSwitch ( projectOptions ) ;
370375 const { projectKeys } = await InternalApiKey . createAndSetProjectKeys ( project . adminAccessToken ) ;
371376 if ( ! projectKeys . superSecretAdminKey ) {
@@ -376,10 +381,12 @@ export async function createProjectWithExternalDb(externalDatabases: any, projec
376381 } ) ;
377382
378383 // Track this project for cleanup
379- createdProjects . push ( {
380- projectId : project . projectId ,
381- superSecretAdminKey : projectKeys . superSecretAdminKey ,
382- } ) ;
384+ if ( options ?. projectTracker ) {
385+ options . projectTracker . push ( {
386+ projectId : project . projectId ,
387+ superSecretAdminKey : projectKeys . superSecretAdminKey ,
388+ } ) ;
389+ }
383390
384391 return project ;
385392}
@@ -400,8 +407,8 @@ export async function cleanupProjectExternalDb() {
400407 * Note: This function makes direct HTTP calls instead of using backendContext
401408 * because it runs in afterAll, which is outside the test context.
402409 */
403- export async function cleanupAllProjectConfigs ( ) {
404- for ( const project of createdProjects ) {
410+ export async function cleanupProjectConfigs ( projects : ProjectContext [ ] ) {
411+ for ( const project of projects ) {
405412 try {
406413 // Make direct HTTP call to clear the external DB config
407414 await niceFetch ( new URL ( '/api/latest/internal/config/override' , STACK_BACKEND_BASE_URL ) , {
@@ -421,7 +428,4 @@ export async function cleanupAllProjectConfigs() {
421428 console . warn ( `Failed to cleanup project ${ project . projectId } :` , err ) ;
422429 }
423430 }
424-
425- // Clear the tracked projects
426- createdProjects . length = 0 ;
427431}
0 commit comments