diff --git a/packages/mock/src/creator.ts b/packages/mock/src/creator.ts index f1ca0aedcc0f..16bc4eebe200 100644 --- a/packages/mock/src/creator.ts +++ b/packages/mock/src/creator.ts @@ -21,7 +21,11 @@ import { } from '@midwayjs/core'; import { isAbsolute, join, resolve } from 'path'; import { clearAllLoggers } from '@midwayjs/logger'; -import { ComponentModule, MockAppConfigurationOptions } from './interface'; +import { + ComponentModule, + MockAppConfigurationOptions, + IBootstrapAppStarter, +} from './interface'; import { findFirstExistModule, isTestEnvironment, @@ -213,8 +217,8 @@ export async function createApp< return framework.getApplication(); } -export async function close>( - app: T, +export async function close( + app: IMidwayApplication | { close: (...args) => void }, options?: { cleanLogsDir?: boolean; cleanTempDir?: boolean; @@ -222,24 +226,32 @@ export async function close>( } ) { if (!app) return; - debug(`[mock]: Closing app, appDir=${app.getAppDir()}`); - options = options || {}; - - await destroyGlobalApplicationContext(app.getApplicationContext()); - if (isTestEnvironment()) { - // clean first - if (options.cleanLogsDir && !isWin32()) { - await removeFile(join(app.getAppDir(), 'logs')); - } - if (MidwayFrameworkType.WEB === app.getFrameworkType()) { - if (options.cleanTempDir && !isWin32()) { - await removeFile(join(app.getAppDir(), 'run')); + if ( + app instanceof BootstrapAppStarter || + (typeof app['close'] === 'function' && !app['getConfig']) + ) { + await app['close'](options); + } else { + app = app as IMidwayApplication; + debug(`[mock]: Closing app, appDir=${app.getAppDir()}`); + options = options || {}; + + await destroyGlobalApplicationContext(app.getApplicationContext()); + if (isTestEnvironment()) { + // clean first + if (options.cleanLogsDir && !isWin32()) { + await removeFile(join(app.getAppDir(), 'logs')); + } + if (MidwayFrameworkType.WEB === app.getFrameworkType()) { + if (options.cleanTempDir && !isWin32()) { + await removeFile(join(app.getAppDir(), 'run')); + } + } + if (options.sleep > 0) { + await sleep(options.sleep); + } else { + await sleep(50); } - } - if (options.sleep > 0) { - await sleep(options.sleep); - } else { - await sleep(50); } } } @@ -520,7 +532,7 @@ class LightFramework extends BaseFramework { } } -class BootstrapAppStarter { +class BootstrapAppStarter implements IBootstrapAppStarter { getApp(type: MidwayFrameworkType | string): IMidwayApplication { const applicationContext = getCurrentApplicationContext(); const applicationManager = applicationContext.get(MidwayApplicationManager); @@ -577,10 +589,15 @@ export async function createLightApp( export async function createBootstrap( entryFile: string, options: MockAppConfigurationOptions = {} -): Promise { +): Promise { if (safeRequire('@midwayjs/faas')) { options.entryFile = entryFile; - return createFunctionApp(process.cwd(), options); + const app = await createFunctionApp(process.cwd(), options); + return { + close: async () => { + return close(app); + }, + }; } else { await create(undefined, { entryFile, diff --git a/packages/mock/src/interface.ts b/packages/mock/src/interface.ts index 4233786ed4ae..eaefdd37d690 100644 --- a/packages/mock/src/interface.ts +++ b/packages/mock/src/interface.ts @@ -1,4 +1,4 @@ -import { IMidwayBootstrapOptions } from '@midwayjs/core'; +import { IMidwayApplication, IMidwayBootstrapOptions, MidwayFrameworkType } from '@midwayjs/core'; export interface MockAppConfigurationOptions extends IMidwayBootstrapOptions { cleanLogsDir?: boolean; @@ -10,3 +10,8 @@ export interface MockAppConfigurationOptions extends IMidwayBootstrapOptions { export type ComponentModule = { Configuration: new () => any; }; + +export interface IBootstrapAppStarter { + getApp?(type: MidwayFrameworkType | string): IMidwayApplication; + close(options?: { sleep?: number }): Promise; +}