From 88b3caadd24f08221b8098c42733e26376338cae Mon Sep 17 00:00:00 2001 From: Haoliang Gao Date: Thu, 13 Jul 2023 13:05:53 +0800 Subject: [PATCH] fix: after call mockModuleContext, hasMockModuleContext should be true (#134) * fix: after call mockModuleContext, hasMockModuleContext should be true * f --- plugin/eventbus/test/eventbus.test.ts | 138 +++++++++--------- .../tegg/app/extend/application.unittest.ts | 4 +- .../app/extend/application.unittest.test.ts | 12 ++ 3 files changed, 84 insertions(+), 70 deletions(-) diff --git a/plugin/eventbus/test/eventbus.test.ts b/plugin/eventbus/test/eventbus.test.ts index 71cf9ffe..73046948 100644 --- a/plugin/eventbus/test/eventbus.test.ts +++ b/plugin/eventbus/test/eventbus.test.ts @@ -1,13 +1,12 @@ import assert from 'assert'; import path from 'path'; -import mm from 'egg-mock'; +import mm, { MockApplication } from 'egg-mock'; import { TimerUtil } from '@eggjs/tegg-common-util'; import { HelloService } from './fixtures/apps/event-app/app/event-module/HelloService'; import { HelloLogger } from './fixtures/apps/event-app/app/event-module/HelloLogger'; describe('test/eventbus.test.ts', () => { - let app; - let ctx; + let app: MockApplication; afterEach(async () => { mm.restore(); @@ -30,85 +29,86 @@ describe('test/eventbus.test.ts', () => { }); it('msg should work', async () => { - ctx = await app.mockModuleContext(); - const helloService = await ctx.getEggObject(HelloService); - let msg: string | undefined; - // helloLogger is in child context - mm(HelloLogger.prototype, 'handle', m => { - msg = m; + await app.mockModuleContextScope(async ctx => { + const helloService = await ctx.getEggObject(HelloService); + let msg: string | undefined; + // helloLogger is in child context + mm(HelloLogger.prototype, 'handle', m => { + msg = m; + }); + const eventWaiter = await app.getEventWaiter(); + const helloEvent = eventWaiter.await('helloEgg'); + helloService.hello(); + await helloEvent; + assert(msg === '01'); }); - const eventWaiter = await app.getEventWaiter(); - const helloEvent = eventWaiter.await('helloEgg'); - helloService.hello(); - await helloEvent; - assert(msg === '01'); }); it('cork/uncork should work', async () => { - ctx = await app.mockModuleContext(); - - const helloService = await ctx.getEggObject(HelloService); - let helloTime = 0; - // helloLogger is in child context - mm(HelloLogger.prototype, 'handle', () => { - helloTime = Date.now(); + await app.mockModuleContextScope(async ctx => { + const helloService = await ctx.getEggObject(HelloService); + let helloTime = 0; + // helloLogger is in child context + mm(HelloLogger.prototype, 'handle', () => { + helloTime = Date.now(); + }); + helloService.cork(); + const triggerTime = Date.now(); + helloService.hello(); + + await TimerUtil.sleep(100); + helloService.uncork(); + + const eventWaiter = await app.getEventWaiter(); + const helloEvent = eventWaiter.await('helloEgg'); + await helloEvent; + assert(helloTime >= triggerTime + 100); }); - helloService.cork(); - const triggerTime = Date.now(); - helloService.hello(); - - await TimerUtil.sleep(100); - helloService.uncork(); - - const eventWaiter = await app.getEventWaiter(); - const helloEvent = eventWaiter.await('helloEgg'); - await helloEvent; - assert(helloTime >= triggerTime + 100); }); it('can call cork/uncork multi times', async () => { - ctx = await app.mockModuleContext(); - - const helloService = await ctx.getEggObject(HelloService); - const eventWaiter = await app.getEventWaiter(); - - let helloCalled = 0; - // helloLogger is in child context - mm(HelloLogger.prototype, 'handle', () => { - helloCalled++; + await app.mockModuleContextScope(async ctx => { + const helloService = await ctx.getEggObject(HelloService); + const eventWaiter = await app.getEventWaiter(); + + let helloCalled = 0; + // helloLogger is in child context + mm(HelloLogger.prototype, 'handle', () => { + helloCalled++; + }); + helloService.cork(); + helloService.hello(); + helloService.uncork(); + await eventWaiter.await('helloEgg'); + + helloService.cork(); + helloService.hello(); + helloService.uncork(); + await eventWaiter.await('helloEgg'); + + assert(helloCalled === 2); }); - helloService.cork(); - helloService.hello(); - helloService.uncork(); - await eventWaiter.await('helloEgg'); - - helloService.cork(); - helloService.hello(); - helloService.uncork(); - await eventWaiter.await('helloEgg'); - - assert(helloCalled === 2); }); it('reentry cork/uncork should work', async () => { - ctx = await app.mockModuleContext(); - - const helloService = await ctx.getEggObject(HelloService); - const eventWaiter = await app.getEventWaiter(); - - let helloCalled = 0; - // helloLogger is in child context - mm(HelloLogger.prototype, 'handle', () => { - helloCalled++; + await app.mockModuleContextScope(async ctx => { + const helloService = await ctx.getEggObject(HelloService); + const eventWaiter = await app.getEventWaiter(); + + let helloCalled = 0; + // helloLogger is in child context + mm(HelloLogger.prototype, 'handle', () => { + helloCalled++; + }); + helloService.cork(); + helloService.cork(); + helloService.hello(); + helloService.uncork(); + helloService.uncork(); + await eventWaiter.await('helloEgg'); + + assert(helloCalled === 1); }); - helloService.cork(); - helloService.cork(); - helloService.hello(); - helloService.uncork(); - helloService.uncork(); - await eventWaiter.await('helloEgg'); - - assert(helloCalled === 1); }); it('concurrent cork/uncork should work', async () => { diff --git a/plugin/tegg/app/extend/application.unittest.ts b/plugin/tegg/app/extend/application.unittest.ts index 822d2e31..c9d49954 100644 --- a/plugin/tegg/app/extend/application.unittest.ts +++ b/plugin/tegg/app/extend/application.unittest.ts @@ -9,8 +9,9 @@ let hasMockModuleContext = false; export default { async mockModuleContext(this: MockApplication, data?: any): Promise { + this.deprecate('app.mockModuleContext is deprecated, use mockModuleContextScope.'); if (hasMockModuleContext) { - throw new Error('should not call mockModuleContext twice, should use mockModuleContextScope.'); + throw new Error('should not call mockModuleContext twice.'); } const ctx = this.mockContext(data); const teggCtx = new EggContextImpl(ctx); @@ -19,6 +20,7 @@ export default { if (teggCtx.init) { await teggCtx.init(lifecycle); } + hasMockModuleContext = true; return ctx; }, diff --git a/plugin/tegg/test/app/extend/application.unittest.test.ts b/plugin/tegg/test/app/extend/application.unittest.test.ts index 4372869f..2e1376e0 100644 --- a/plugin/tegg/test/app/extend/application.unittest.test.ts +++ b/plugin/tegg/test/app/extend/application.unittest.test.ts @@ -31,4 +31,16 @@ describe('test/app/extend/application.unittest.test.ts', () => { assert(traceId); }); }); + + it('should not call mockModuleContext twice', async () => { + const ctx = await app.mockModuleContext(); + try { + await assert.rejects( + app.mockModuleContext(), + /should not call mockModuleContext twice./, + ); + } finally { + await app.destroyModuleContext(ctx); + } + }); });