1
1
2
- const assert = require ( 'assert' ) ;
3
- const fs = require ( 'fs' ) ;
4
- const path = require ( 'path' ) ;
5
- const utils = require ( '../../utils' ) ;
2
+ import { strict as assert } from 'node:assert' ;
3
+ import fs from 'node:fs' ;
4
+ import path from 'node:path' ;
5
+ import { scheduler } from 'node:timers/promises' ;
6
+ import { createApp , MockApplication , cluster } from '../../utils.js' ;
6
7
7
- describe ( 'test/app/extend/application.test.js ' , ( ) => {
8
+ describe ( 'test/app/extend/application.test.ts ' , ( ) => {
8
9
describe ( 'app.logger' , ( ) => {
9
- let app ;
10
+ let app : MockApplication ;
10
11
before ( ( ) => {
11
- app = utils . app ( 'apps/demo' ) ;
12
+ app = createApp ( 'apps/demo' ) ;
12
13
return app . ready ( ) ;
13
14
} ) ;
14
15
after ( ( ) => app . close ( ) ) ;
@@ -17,23 +18,23 @@ describe('test/app/extend/application.test.js', () => {
17
18
assert ( app . logger === app . loggers . logger ) ;
18
19
} ) ;
19
20
20
- it ( 'should alias app.coreLooger => app.loggers.coreLooger ' , ( ) => {
21
+ it ( 'should alias app.coreLogger => app.loggers.coreLogger ' , ( ) => {
21
22
assert ( app . coreLogger === app . loggers . coreLogger ) ;
22
23
} ) ;
23
24
24
- it ( 'should alias app.getLogger(\'coreLogger\') => app.loggers.coreLooger ' , ( ) => {
25
+ it ( 'should alias app.getLogger(\'coreLogger\') => app.loggers.coreLogger ' , ( ) => {
25
26
assert ( app . getLogger ( 'coreLogger' ) === app . loggers . coreLogger ) ;
26
27
} ) ;
27
28
28
- it ( 'should alias app.getLogger(\'noexist \') => null' , ( ) => {
29
- assert ( app . getLogger ( 'noexist ' ) === null ) ;
29
+ it ( 'should alias app.getLogger(\'noExist \') => null' , ( ) => {
30
+ assert ( app . getLogger ( 'noExist ' ) === null ) ;
30
31
} ) ;
31
32
} ) ;
32
33
33
34
describe ( 'app.inspect()' , ( ) => {
34
- let app ;
35
+ let app : MockApplication ;
35
36
before ( ( ) => {
36
- app = utils . app ( 'apps/demo' ) ;
37
+ app = createApp ( 'apps/demo' ) ;
37
38
return app . ready ( ) ;
38
39
} ) ;
39
40
after ( ( ) => app . close ( ) ) ;
@@ -50,22 +51,22 @@ describe('test/app/extend/application.test.js', () => {
50
51
} ) ;
51
52
52
53
describe ( 'app.readyCallback()' , ( ) => {
53
- let app ;
54
+ let app : MockApplication ;
54
55
after ( ( ) => app . close ( ) ) ;
55
56
56
57
it ( 'should log info when plugin is not ready' , async ( ) => {
57
- app = utils . cluster ( 'apps/notready' ) ;
58
+ app = cluster ( 'apps/notready' ) ;
58
59
// it won't be ready, so wait for the timeout
59
- await utils . sleep ( 11000 ) ;
60
+ await scheduler . wait ( 11000 ) ;
60
61
61
62
app . expect ( 'stderr' , / \[ e g g : c o r e : r e a d y _ t i m e o u t ] 1 0 s e c o n d s l a t e r a w a s s t i l l u n a b l e t o f i n i s h ./ ) ;
62
63
} ) ;
63
64
} ) ;
64
65
65
66
describe ( 'app.locals' , ( ) => {
66
- let app ;
67
+ let app : MockApplication ;
67
68
before ( ( ) => {
68
- app = utils . app ( 'apps/locals' ) ;
69
+ app = createApp ( 'apps/locals' ) ;
69
70
return app . ready ( ) ;
70
71
} ) ;
71
72
after ( ( ) => app . close ( ) ) ;
@@ -84,15 +85,15 @@ describe('test/app/extend/application.test.js', () => {
84
85
} ) ;
85
86
86
87
describe ( 'app.locals.foo = bar' , ( ) => {
87
- let app ;
88
+ let app : MockApplication ;
88
89
before ( ( ) => {
89
- app = utils . app ( 'apps/app-locals-getter' ) ;
90
+ app = createApp ( 'apps/app-locals-getter' ) ;
90
91
return app . ready ( ) ;
91
92
} ) ;
92
93
after ( ( ) => app . close ( ) ) ;
93
94
94
- it ( 'should work' , ( ) => {
95
- return app . httpRequest ( )
95
+ it ( 'should work' , async ( ) => {
96
+ return await app . httpRequest ( )
96
97
. get ( '/test' )
97
98
. expect ( {
98
99
locals : {
@@ -104,9 +105,9 @@ describe('test/app/extend/application.test.js', () => {
104
105
} ) ;
105
106
106
107
describe ( 'app.createAnonymousContext()' , ( ) => {
107
- let app ;
108
+ let app : MockApplication ;
108
109
before ( ( ) => {
109
- app = utils . app ( 'apps/demo' ) ;
110
+ app = createApp ( 'apps/demo' ) ;
110
111
return app . ready ( ) ;
111
112
} ) ;
112
113
after ( ( ) => app . close ( ) ) ;
@@ -120,7 +121,7 @@ describe('test/app/extend/application.test.js', () => {
120
121
'x-forwarded-for' : '10.0.0.1' ,
121
122
} ,
122
123
url : '/foobar?ok=1' ,
123
- } ) ;
124
+ } as any ) ;
124
125
assert ( ctx . ip === '10.0.0.1' ) ;
125
126
assert ( ctx . url === '/foobar?ok=1' ) ;
126
127
assert ( ctx . socket . remoteAddress === '10.0.0.1' ) ;
@@ -129,9 +130,9 @@ describe('test/app/extend/application.test.js', () => {
129
130
} ) ;
130
131
131
132
describe ( 'app.addSingleton()' , ( ) => {
132
- let app ;
133
+ let app : MockApplication ;
133
134
before ( ( ) => {
134
- app = utils . app ( 'apps/singleton-demo' ) ;
135
+ app = createApp ( 'apps/singleton-demo' ) ;
135
136
return app . ready ( ) ;
136
137
} ) ;
137
138
after ( ( ) => app . close ( ) ) ;
@@ -156,7 +157,7 @@ describe('test/app/extend/application.test.js', () => {
156
157
try {
157
158
app . dataServiceAsync . createInstance ( { foo : 'barrr' } ) ;
158
159
throw new Error ( 'should not execute' ) ;
159
- } catch ( err ) {
160
+ } catch ( err : any ) {
160
161
assert ( err . message === 'egg:singleton dataServiceAsync only support create asynchronous, please use createInstanceAsync' ) ;
161
162
}
162
163
@@ -167,9 +168,9 @@ describe('test/app/extend/application.test.js', () => {
167
168
} ) ;
168
169
169
170
describe ( 'app.runInBackground(scope)' , ( ) => {
170
- let app ;
171
+ let app : MockApplication ;
171
172
before ( ( ) => {
172
- app = utils . app ( 'apps/ctx-background' ) ;
173
+ app = createApp ( 'apps/ctx-background' ) ;
173
174
return app . ready ( ) ;
174
175
} ) ;
175
176
after ( ( ) => app . close ( ) ) ;
@@ -179,72 +180,72 @@ describe('test/app/extend/application.test.js', () => {
179
180
. get ( '/app_background' )
180
181
. expect ( 200 )
181
182
. expect ( 'hello app' ) ;
182
- await utils . sleep ( 2100 ) ;
183
+ await scheduler . wait ( 2100 ) ;
183
184
const logdir = app . config . logger . dir ;
184
185
const log = fs . readFileSync ( path . join ( logdir , 'ctx-background-web.log' ) , 'utf8' ) ;
185
186
assert ( / m o c k b a c k g r o u n d r u n a t a p p r e s u l t f i l e s i z e : \d + / . test ( log ) ) ;
186
187
assert ( / m o c k b a c k g r o u n d r u n a t a p p a n o n y m o u s r e s u l t f i l e s i z e : \d + / . test ( log ) ) ;
187
188
assert (
188
- / \[ e g g : b a c k g r o u n d ] t a s k : .* ?a p p [ \/ \\ ] c o n t r o l l e r [ \/ \\ ] a p p \. j s : \d + : \d + s u c c e s s \( [ \d \. ] + m s \) / . test ( fs . readFileSync ( path . join ( logdir , 'egg-web.log' ) , 'utf8' ) )
189
+ / \[ e g g : b a c k g r o u n d ] t a s k : .* ?a p p [ \/ \\ ] c o n t r o l l e r [ \/ \\ ] a p p \. j s : \d + : \d + s u c c e s s \( [ \d \. ] + m s \) / . test ( fs . readFileSync ( path . join ( logdir , 'egg-web.log' ) , 'utf8' ) ) ,
189
190
) ;
190
191
} ) ;
191
192
} ) ;
192
193
193
194
describe ( 'app.runInAnonymousContextScope(scope)' , ( ) => {
194
195
it ( 'should run task in anonymous context scope success' , async ( ) => {
195
- const app = utils . app ( 'apps/app-runInAnonymousContextScope' ) ;
196
+ const app = createApp ( 'apps/app-runInAnonymousContextScope' ) ;
196
197
await app . ready ( ) ;
197
198
await app . close ( ) ;
198
- await utils . sleep ( 2100 ) ;
199
+ await scheduler . wait ( 2100 ) ;
199
200
const logdir = app . config . logger . dir ;
200
201
const logs = fs . readFileSync ( path . join ( logdir , 'app-runInAnonymousContextScope-web.log' ) , 'utf8' ) . split ( '\n' ) ;
201
202
// console.log(logs);
202
203
// 2022-12-15 23:00:08,551 INFO 86728 [-/127.0.0.1/-/1ms GET /] before close on ctx logger
203
204
// 2022-12-15 23:00:08,551 INFO 86728 [-/127.0.0.1/-/1ms GET /] before close on app logger
204
205
// 2022-12-15 23:03:16,086 INFO 89216 outside before close on app logger
205
- assert . match ( logs [ 0 ] , / I N F O \d + \[ - \/ 1 2 7 .0 .0 .1 \/ - \/ \d + m s G E T \/ ] i n s i d e b e f o r e c l o s e o n c t x l o g g e r / ) ;
206
- assert . match ( logs [ 1 ] , / I N F O \d + \[ - \/ 1 2 7 .0 .0 .1 \/ - \/ \d + m s G E T \/ ] i n s i d e b e f o r e c l o s e o n a p p l o g g e r / ) ;
206
+ assert . match ( logs [ 0 ] , / I N F O \d + \[ - \/ 1 2 7 .0 .0 .1 \/ - \/ [ \d \. ] + m s G E T \/ ] i n s i d e b e f o r e c l o s e o n c t x l o g g e r / ) ;
207
+ assert . match ( logs [ 1 ] , / I N F O \d + \[ - \/ 1 2 7 .0 .0 .1 \/ - \/ [ \d \. ] + m s G E T \/ ] i n s i d e b e f o r e c l o s e o n a p p l o g g e r / ) ;
207
208
assert . match ( logs [ 2 ] , / I N F O \d + o u t s i d e b e f o r e c l o s e o n a p p l o g g e r / ) ;
208
209
} ) ;
209
210
} ) ;
210
211
211
212
describe ( 'app.runInAnonymousContextScope(scope,request)' , ( ) => {
212
213
it ( 'should run task in anonymous context scope with req success' , async ( ) => {
213
- const app = utils . app ( 'apps/app-runInAnonymousContextScope-withRequest' ) ;
214
+ const app = createApp ( 'apps/app-runInAnonymousContextScope-withRequest' ) ;
214
215
await app . ready ( ) ;
215
216
await app . close ( ) ;
216
- await utils . sleep ( 2100 ) ;
217
+ await scheduler . wait ( 2100 ) ;
217
218
const logdir = app . config . logger . dir ;
218
219
const logs = fs . readFileSync ( path . join ( logdir , 'app-runInAnonymousContextScope-withRequest-web.log' ) , { encoding : 'utf8' } ) . split ( '\n' ) ;
219
220
220
- assert . match ( logs [ 0 ] , / I N F O \d + \[ - \/ 1 2 7 .0 .0 .2 \/ - \/ \d + m s G E T \/ ] i n s i d e b e f o r e c l o s e o n c t x l o g g e r / ) ;
221
- assert . match ( logs [ 1 ] , / I N F O \d + \[ - \/ 1 2 7 .0 .0 .2 \/ - \/ \d + m s G E T \/ ] i n s i d e b e f o r e c l o s e o n a p p l o g g e r / ) ;
221
+ assert . match ( logs [ 0 ] , / I N F O \d + \[ - \/ 1 2 7 .0 .0 .2 \/ - \/ [ \d \. ] + m s G E T \/ ] i n s i d e b e f o r e c l o s e o n c t x l o g g e r / ) ;
222
+ assert . match ( logs [ 1 ] , / I N F O \d + \[ - \/ 1 2 7 .0 .0 .2 \/ - \/ [ \d \. ] + m s G E T \/ ] i n s i d e b e f o r e c l o s e o n a p p l o g g e r / ) ;
222
223
assert . match ( logs [ 2 ] , / I N F O \d + o u t s i d e b e f o r e c l o s e o n a p p l o g g e r / ) ;
223
224
} ) ;
224
225
} ) ;
225
226
226
227
describe ( 'app.handleRequest(ctx, fnMiddleware)' , ( ) => {
227
- let app ;
228
+ let app : MockApplication ;
228
229
before ( ( ) => {
229
- app = utils . app ( 'apps/demo' ) ;
230
+ app = createApp ( 'apps/demo' ) ;
230
231
return app . ready ( ) ;
231
232
} ) ;
232
233
after ( ( ) => app . close ( ) ) ;
233
234
234
235
it ( 'should wait for middleware resolution' , async ( ) => {
235
236
const ctx = app . createAnonymousContext ( ) ;
236
- await app . handleRequest ( ctx , async ctx => {
237
- await utils . sleep ( 100 ) ;
237
+ await app . handleRequest ( ctx , async ( ctx : any ) => {
238
+ await scheduler . wait ( 100 ) ;
238
239
ctx . body = 'middleware resolution' ;
239
240
} ) ;
240
241
assert ( ctx . body === 'middleware resolution' ) ;
241
242
} ) ;
242
243
} ) ;
243
244
244
245
describe ( 'app.keys' , ( ) => {
245
- let app ;
246
+ let app : MockApplication ;
246
247
before ( ( ) => {
247
- app = utils . app ( 'apps/demo' ) ;
248
+ app = createApp ( 'apps/demo' ) ;
248
249
return app . ready ( ) ;
249
250
} ) ;
250
251
after ( ( ) => app . close ( ) ) ;
0 commit comments