@@ -34,11 +34,10 @@ const CLIENT_CONFIG: Partial<ClientConfig> = {
3434} ;
3535
3636// Track all projects created with external DB configs for cleanup
37- type ProjectContext = {
37+ export type ProjectContext = {
3838 projectId : string ,
3939 superSecretAdminKey : string ,
4040} ;
41- const createdProjects : ProjectContext [ ] = [ ] ;
4241
4342/**
4443 * Helper class to manage external test databases
@@ -47,6 +46,7 @@ export class TestDbManager {
4746 private setupClient : Client | null = null ;
4847 private databases : Map < string , Client > = new Map ( ) ;
4948 private databaseNames : Set < string > = new Set ( ) ;
49+ public readonly createdProjects : ProjectContext [ ] = [ ] ;
5050
5151 async init ( ) {
5252 this . setupClient = new Client ( {
@@ -81,7 +81,8 @@ export class TestDbManager {
8181
8282 async cleanup ( ) {
8383 // First, clean up all project configs to stop the sync cron from trying to connect
84- await cleanupAllProjectConfigs ( ) ;
84+ await cleanupProjectConfigs ( this . createdProjects ) ;
85+ this . createdProjects . length = 0 ;
8586
8687 // Close all tracked database clients
8788 const closePromises = Array . from ( this . databases . values ( ) ) . map ( async ( client ) => {
@@ -321,7 +322,11 @@ export async function countUsersInExternalDb(client: Client): Promise<number> {
321322 * Helper to create a project and update its config with external DB settings.
322323 * Tracks the project for cleanup later.
323324 */
324- export async function createProjectWithExternalDb ( externalDatabases : any , projectOptions ?: { display_name ?: string , description ?: string } ) {
325+ export async function createProjectWithExternalDb (
326+ externalDatabases : any ,
327+ projectOptions ?: { display_name ?: string , description ?: string } ,
328+ options ?: { projectTracker ?: ProjectContext [ ] }
329+ ) {
325330 const project = await Project . createAndSwitch ( projectOptions ) ;
326331 const { projectKeys } = await InternalApiKey . createAndSetProjectKeys ( project . adminAccessToken ) ;
327332 if ( ! projectKeys . superSecretAdminKey ) {
@@ -332,10 +337,12 @@ export async function createProjectWithExternalDb(externalDatabases: any, projec
332337 } ) ;
333338
334339 // Track this project for cleanup
335- createdProjects . push ( {
336- projectId : project . projectId ,
337- superSecretAdminKey : projectKeys . superSecretAdminKey ,
338- } ) ;
340+ if ( options ?. projectTracker ) {
341+ options . projectTracker . push ( {
342+ projectId : project . projectId ,
343+ superSecretAdminKey : projectKeys . superSecretAdminKey ,
344+ } ) ;
345+ }
339346
340347 return project ;
341348}
@@ -356,8 +363,8 @@ export async function cleanupProjectExternalDb() {
356363 * Note: This function makes direct HTTP calls instead of using backendContext
357364 * because it runs in afterAll, which is outside the test context.
358365 */
359- export async function cleanupAllProjectConfigs ( ) {
360- for ( const project of createdProjects ) {
366+ export async function cleanupProjectConfigs ( projects : ProjectContext [ ] ) {
367+ for ( const project of projects ) {
361368 try {
362369 // Make direct HTTP call to clear the external DB config
363370 await niceFetch ( new URL ( '/api/latest/internal/config/override' , STACK_BACKEND_BASE_URL ) , {
@@ -377,7 +384,4 @@ export async function cleanupAllProjectConfigs() {
377384 console . warn ( `Failed to cleanup project ${ project . projectId } :` , err ) ;
378385 }
379386 }
380-
381- // Clear the tracked projects
382- createdProjects . length = 0 ;
383387}
0 commit comments