Skip to content

Commit

Permalink
fix: set bootstrap app to global (#180)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **Chores**
- Enhanced application instance management by introducing a global
variable for centralized access across different function calls.
- Improved application setup and retrieval mechanisms to support testing
frameworks like Mocha and Jest.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
fengmk2 authored Dec 29, 2024
1 parent 8f70532 commit 4e1525c
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/lib/app_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ import ApplicationUnittest from '../app/extend/application.js';

const debug = debuglog('@eggjs/mock/lib/app_handler');

let app: ApplicationUnittest;
declare namespace globalThis {
let __eggMockAppInstance: ApplicationUnittest | null;
}

globalThis.__eggMockAppInstance = null;

export function setupApp() {
let app = globalThis.__eggMockAppInstance!;
if (app) {
debug('return exists app');
return app;
Expand Down Expand Up @@ -46,6 +51,7 @@ export function setupApp() {
afterEach(restore);
}
}
globalThis.__eggMockAppInstance = app;
return app;
}

Expand All @@ -59,12 +65,13 @@ export async function getApp(suite?: unknown, test?: unknown) {
if (getAppCallback) {
return getAppCallback(suite, test);
}
const app = globalThis.__eggMockAppInstance!;
if (app) {
await app.ready();
}
return app;
}

export function getBootstrapApp() {
return app;
return globalThis.__eggMockAppInstance!;
}

0 comments on commit 4e1525c

Please sign in to comment.