Skip to content

Commit 88b3caa

Browse files
authored
fix: after call mockModuleContext, hasMockModuleContext should be true (#134)
* fix: after call mockModuleContext, hasMockModuleContext should be true * f
1 parent 6039fbc commit 88b3caa

File tree

3 files changed

+84
-70
lines changed

3 files changed

+84
-70
lines changed

plugin/eventbus/test/eventbus.test.ts

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import assert from 'assert';
22
import path from 'path';
3-
import mm from 'egg-mock';
3+
import mm, { MockApplication } from 'egg-mock';
44
import { TimerUtil } from '@eggjs/tegg-common-util';
55
import { HelloService } from './fixtures/apps/event-app/app/event-module/HelloService';
66
import { HelloLogger } from './fixtures/apps/event-app/app/event-module/HelloLogger';
77

88
describe('test/eventbus.test.ts', () => {
9-
let app;
10-
let ctx;
9+
let app: MockApplication;
1110

1211
afterEach(async () => {
1312
mm.restore();
@@ -30,85 +29,86 @@ describe('test/eventbus.test.ts', () => {
3029
});
3130

3231
it('msg should work', async () => {
33-
ctx = await app.mockModuleContext();
34-
const helloService = await ctx.getEggObject(HelloService);
35-
let msg: string | undefined;
36-
// helloLogger is in child context
37-
mm(HelloLogger.prototype, 'handle', m => {
38-
msg = m;
32+
await app.mockModuleContextScope(async ctx => {
33+
const helloService = await ctx.getEggObject(HelloService);
34+
let msg: string | undefined;
35+
// helloLogger is in child context
36+
mm(HelloLogger.prototype, 'handle', m => {
37+
msg = m;
38+
});
39+
const eventWaiter = await app.getEventWaiter();
40+
const helloEvent = eventWaiter.await('helloEgg');
41+
helloService.hello();
42+
await helloEvent;
43+
assert(msg === '01');
3944
});
40-
const eventWaiter = await app.getEventWaiter();
41-
const helloEvent = eventWaiter.await('helloEgg');
42-
helloService.hello();
43-
await helloEvent;
44-
assert(msg === '01');
4545
});
4646

4747
it('cork/uncork should work', async () => {
48-
ctx = await app.mockModuleContext();
49-
50-
const helloService = await ctx.getEggObject(HelloService);
51-
let helloTime = 0;
52-
// helloLogger is in child context
53-
mm(HelloLogger.prototype, 'handle', () => {
54-
helloTime = Date.now();
48+
await app.mockModuleContextScope(async ctx => {
49+
const helloService = await ctx.getEggObject(HelloService);
50+
let helloTime = 0;
51+
// helloLogger is in child context
52+
mm(HelloLogger.prototype, 'handle', () => {
53+
helloTime = Date.now();
54+
});
55+
helloService.cork();
56+
const triggerTime = Date.now();
57+
helloService.hello();
58+
59+
await TimerUtil.sleep(100);
60+
helloService.uncork();
61+
62+
const eventWaiter = await app.getEventWaiter();
63+
const helloEvent = eventWaiter.await('helloEgg');
64+
await helloEvent;
65+
assert(helloTime >= triggerTime + 100);
5566
});
56-
helloService.cork();
57-
const triggerTime = Date.now();
58-
helloService.hello();
59-
60-
await TimerUtil.sleep(100);
61-
helloService.uncork();
62-
63-
const eventWaiter = await app.getEventWaiter();
64-
const helloEvent = eventWaiter.await('helloEgg');
65-
await helloEvent;
66-
assert(helloTime >= triggerTime + 100);
6767
});
6868

6969
it('can call cork/uncork multi times', async () => {
70-
ctx = await app.mockModuleContext();
71-
72-
const helloService = await ctx.getEggObject(HelloService);
73-
const eventWaiter = await app.getEventWaiter();
74-
75-
let helloCalled = 0;
76-
// helloLogger is in child context
77-
mm(HelloLogger.prototype, 'handle', () => {
78-
helloCalled++;
70+
await app.mockModuleContextScope(async ctx => {
71+
const helloService = await ctx.getEggObject(HelloService);
72+
const eventWaiter = await app.getEventWaiter();
73+
74+
let helloCalled = 0;
75+
// helloLogger is in child context
76+
mm(HelloLogger.prototype, 'handle', () => {
77+
helloCalled++;
78+
});
79+
helloService.cork();
80+
helloService.hello();
81+
helloService.uncork();
82+
await eventWaiter.await('helloEgg');
83+
84+
helloService.cork();
85+
helloService.hello();
86+
helloService.uncork();
87+
await eventWaiter.await('helloEgg');
88+
89+
assert(helloCalled === 2);
7990
});
80-
helloService.cork();
81-
helloService.hello();
82-
helloService.uncork();
83-
await eventWaiter.await('helloEgg');
84-
85-
helloService.cork();
86-
helloService.hello();
87-
helloService.uncork();
88-
await eventWaiter.await('helloEgg');
89-
90-
assert(helloCalled === 2);
9191
});
9292

9393
it('reentry cork/uncork should work', async () => {
94-
ctx = await app.mockModuleContext();
95-
96-
const helloService = await ctx.getEggObject(HelloService);
97-
const eventWaiter = await app.getEventWaiter();
98-
99-
let helloCalled = 0;
100-
// helloLogger is in child context
101-
mm(HelloLogger.prototype, 'handle', () => {
102-
helloCalled++;
94+
await app.mockModuleContextScope(async ctx => {
95+
const helloService = await ctx.getEggObject(HelloService);
96+
const eventWaiter = await app.getEventWaiter();
97+
98+
let helloCalled = 0;
99+
// helloLogger is in child context
100+
mm(HelloLogger.prototype, 'handle', () => {
101+
helloCalled++;
102+
});
103+
helloService.cork();
104+
helloService.cork();
105+
helloService.hello();
106+
helloService.uncork();
107+
helloService.uncork();
108+
await eventWaiter.await('helloEgg');
109+
110+
assert(helloCalled === 1);
103111
});
104-
helloService.cork();
105-
helloService.cork();
106-
helloService.hello();
107-
helloService.uncork();
108-
helloService.uncork();
109-
await eventWaiter.await('helloEgg');
110-
111-
assert(helloCalled === 1);
112112
});
113113

114114
it('concurrent cork/uncork should work', async () => {

plugin/tegg/app/extend/application.unittest.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ let hasMockModuleContext = false;
99

1010
export default {
1111
async mockModuleContext(this: MockApplication, data?: any): Promise<Context> {
12+
this.deprecate('app.mockModuleContext is deprecated, use mockModuleContextScope.');
1213
if (hasMockModuleContext) {
13-
throw new Error('should not call mockModuleContext twice, should use mockModuleContextScope.');
14+
throw new Error('should not call mockModuleContext twice.');
1415
}
1516
const ctx = this.mockContext(data);
1617
const teggCtx = new EggContextImpl(ctx);
@@ -19,6 +20,7 @@ export default {
1920
if (teggCtx.init) {
2021
await teggCtx.init(lifecycle);
2122
}
23+
hasMockModuleContext = true;
2224
return ctx;
2325
},
2426

plugin/tegg/test/app/extend/application.unittest.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,16 @@ describe('test/app/extend/application.unittest.test.ts', () => {
3131
assert(traceId);
3232
});
3333
});
34+
35+
it('should not call mockModuleContext twice', async () => {
36+
const ctx = await app.mockModuleContext();
37+
try {
38+
await assert.rejects(
39+
app.mockModuleContext(),
40+
/should not call mockModuleContext twice./,
41+
);
42+
} finally {
43+
await app.destroyModuleContext(ctx);
44+
}
45+
});
3446
});

0 commit comments

Comments
 (0)