1
- const assert = require ( 'assert' ) ;
2
- const Singleton = require ( '../../../lib/core/singleton' ) ;
3
- const { sleep } = require ( '../../utils' ) ;
1
+ import { strict as assert } from 'node:assert' ;
2
+ import { scheduler } from 'node:timers/promises' ;
3
+ import { Singleton } from '../../../src/lib/core/singleton.js' ;
4
+
4
5
class DataService {
5
- constructor ( config ) {
6
+ config : any ;
7
+ constructor ( config : any ) {
6
8
this . config = config ;
7
9
}
8
10
@@ -11,41 +13,39 @@ class DataService {
11
13
}
12
14
}
13
15
14
- function create ( config ) {
16
+ function create ( config : any ) {
15
17
return new DataService ( config ) ;
16
18
}
17
19
18
- async function asyncCreate ( config ) {
19
- await sleep ( 10 ) ;
20
+ async function asyncCreate ( config : any ) {
21
+ await scheduler . wait ( 10 ) ;
20
22
return new DataService ( config ) ;
21
23
}
22
24
23
- describe ( 'test/lib/core/singleton.test.js' , ( ) => {
24
-
25
+ describe ( 'test/lib/core/singleton.test.ts' , ( ) => {
25
26
afterEach ( ( ) => {
26
- delete DataService . prototype . createInstance ;
27
- delete DataService . prototype . createInstanceAsync ;
27
+ delete ( DataService as any ) . prototype . createInstance ;
28
+ delete ( DataService as any ) . prototype . createInstanceAsync ;
28
29
} ) ;
29
30
30
31
describe ( 'sync singleton creation tests' , ( ) => {
31
-
32
32
it ( 'should init with client' , async ( ) => {
33
33
const name = 'dataService' ;
34
34
35
35
const clients = [
36
36
{ foo : 'bar' } ,
37
37
] ;
38
38
for ( const client of clients ) {
39
- const app = { config : { dataService : { client } } } ;
39
+ const app : any = { config : { dataService : { client } } } ;
40
40
const singleton = new Singleton ( {
41
41
name,
42
42
app,
43
43
create,
44
44
} ) ;
45
45
singleton . init ( ) ;
46
46
assert ( app . dataService instanceof DataService ) ;
47
- assert ( app . dataService . config . foo === 'bar' ) ;
48
- assert ( typeof app . dataService . createInstance === 'function' ) ;
47
+ assert . equal ( app . dataService . config . foo , 'bar' ) ;
48
+ assert . equal ( typeof app . dataService . createInstance , 'function' ) ;
49
49
}
50
50
} ) ;
51
51
@@ -57,21 +57,21 @@ describe('test/lib/core/singleton.test.js', () => {
57
57
second : { foo : 'bar2' } ,
58
58
} ;
59
59
60
- const app = { config : { dataService : { clients } } } ;
60
+ const app : any = { config : { dataService : { clients } } } ;
61
61
const singleton = new Singleton ( {
62
62
name,
63
63
app,
64
64
create,
65
65
} ) ;
66
66
singleton . init ( ) ;
67
67
assert ( app . dataService instanceof Singleton ) ;
68
- assert ( app . dataService . get ( 'first' ) . config . foo === 'bar1' ) ;
69
- assert ( app . dataService . get ( 'second' ) . config . foo === 'bar2' ) ;
70
- assert ( typeof app . dataService . createInstance === 'function' ) ;
68
+ assert . equal ( app . dataService . get ( 'first' ) . config . foo , 'bar1' ) ;
69
+ assert . equal ( app . dataService . get ( 'second' ) . config . foo , 'bar2' ) ;
70
+ assert . equal ( typeof app . dataService . createInstance , 'function' ) ;
71
71
} ) ;
72
72
73
73
it ( 'should client support default' , async ( ) => {
74
- const app = {
74
+ const app : any = {
75
75
config : {
76
76
dataService : {
77
77
client : { foo : 'bar' } ,
@@ -88,13 +88,13 @@ describe('test/lib/core/singleton.test.js', () => {
88
88
} ) ;
89
89
singleton . init ( ) ;
90
90
assert ( app . dataService instanceof DataService ) ;
91
- assert ( app . dataService . config . foo === 'bar' ) ;
92
- assert ( app . dataService . config . foo1 === 'bar1' ) ;
93
- assert ( typeof app . dataService . createInstance === 'function' ) ;
91
+ assert . equal ( app . dataService . config . foo , 'bar' ) ;
92
+ assert . equal ( app . dataService . config . foo1 , 'bar1' ) ;
93
+ assert . equal ( typeof app . dataService . createInstance , 'function' ) ;
94
94
} ) ;
95
95
96
96
it ( 'should clients support default' , async ( ) => {
97
- const app = {
97
+ const app : any = {
98
98
config : {
99
99
dataService : {
100
100
clients : {
@@ -124,7 +124,7 @@ describe('test/lib/core/singleton.test.js', () => {
124
124
} ) ;
125
125
126
126
it ( 'should createInstance without client/clients support default' , async ( ) => {
127
- const app = {
127
+ const app : any = {
128
128
config : {
129
129
dataService : {
130
130
default : { foo : 'bar' } ,
@@ -148,12 +148,12 @@ describe('test/lib/core/singleton.test.js', () => {
148
148
} ) ;
149
149
150
150
it ( 'should work with unextensible' , async ( ) => {
151
- function create ( config ) {
151
+ function create ( config : any ) {
152
152
const d = new DataService ( config ) ;
153
153
Object . preventExtensions ( d ) ;
154
154
return d ;
155
155
}
156
- const app = {
156
+ const app : any = {
157
157
config : {
158
158
dataService : {
159
159
client : { foo : 'bar' } ,
@@ -176,12 +176,12 @@ describe('test/lib/core/singleton.test.js', () => {
176
176
} ) ;
177
177
178
178
it ( 'should work with frozen' , async ( ) => {
179
- function create ( config ) {
179
+ function create ( config : any ) {
180
180
const d = new DataService ( config ) ;
181
181
Object . freeze ( d ) ;
182
182
return d ;
183
183
}
184
- const app = {
184
+ const app : any = {
185
185
config : {
186
186
dataService : {
187
187
client : { foo : 'bar' } ,
@@ -211,17 +211,19 @@ describe('test/lib/core/singleton.test.js', () => {
211
211
Object . freeze ( d ) ;
212
212
return d ;
213
213
}
214
- const app = {
214
+ const app : any = {
215
215
config : {
216
216
dataService : {
217
217
client : { foo : 'bar' } ,
218
218
default : { foo : 'bar' } ,
219
219
} ,
220
220
} ,
221
- logger : {
222
- warn ( msg , name ) {
223
- assert ( name === 'dataService' ) ;
224
- warn = true ;
221
+ coreLogger : {
222
+ warn ( _msg : string , name ?: string ) {
223
+ if ( name ) {
224
+ assert . equal ( name , 'dataService' ) ;
225
+ warn = true ;
226
+ }
225
227
} ,
226
228
} ,
227
229
} ;
@@ -243,12 +245,12 @@ describe('test/lib/core/singleton.test.js', () => {
243
245
let success = true ;
244
246
const name = 'dataService' ;
245
247
const clientName = 'customClient' ;
246
- function create ( config , app , client ) {
248
+ function create ( _config : any , _app : any , client : string ) {
247
249
if ( client !== clientName ) {
248
250
success = false ;
249
251
}
250
252
}
251
- const app = {
253
+ const app : any = {
252
254
config : {
253
255
dataService : {
254
256
clients : {
@@ -276,7 +278,7 @@ describe('test/lib/core/singleton.test.js', () => {
276
278
{ foo : 'bar' } ,
277
279
] ;
278
280
for ( const client of clients ) {
279
- const app = { config : { dataService : { client } } } ;
281
+ const app : any = { config : { dataService : { client } } } ;
280
282
const singleton = new Singleton ( {
281
283
name,
282
284
app,
@@ -298,7 +300,7 @@ describe('test/lib/core/singleton.test.js', () => {
298
300
second : { foo : 'bar2' } ,
299
301
} ;
300
302
301
- const app = { config : { dataService : { clients } } } ;
303
+ const app : any = { config : { dataService : { clients } } } ;
302
304
const singleton = new Singleton ( {
303
305
name,
304
306
app,
@@ -312,7 +314,7 @@ describe('test/lib/core/singleton.test.js', () => {
312
314
} ) ;
313
315
314
316
it ( 'should createInstanceAsync without client/clients support default' , async ( ) => {
315
- const app = {
317
+ const app : any = {
316
318
config : {
317
319
dataService : {
318
320
default : { foo : 'bar' } ,
@@ -336,7 +338,7 @@ describe('test/lib/core/singleton.test.js', () => {
336
338
} ) ;
337
339
338
340
it ( 'should createInstanceAsync throw error' , async ( ) => {
339
- const app = {
341
+ const app : any = {
340
342
config : {
341
343
dataService : {
342
344
default : { foo : 'bar' } ,
@@ -356,8 +358,9 @@ describe('test/lib/core/singleton.test.js', () => {
356
358
try {
357
359
app . dataService = await app . dataService . createInstance ( { foo1 : 'bar1' } ) ;
358
360
throw new Error ( 'should not execute' ) ;
359
- } catch ( err ) {
360
- assert ( err . message === 'egg:singleton dataService only support create asynchronous, please use createInstanceAsync' ) ;
361
+ } catch ( err : any ) {
362
+ assert . equal ( err . message ,
363
+ 'egg:singleton dataService only support create asynchronous, please use createInstanceAsync' ) ;
361
364
}
362
365
} ) ;
363
366
@@ -366,12 +369,12 @@ describe('test/lib/core/singleton.test.js', () => {
366
369
const name = 'dataService' ;
367
370
const clientName = 'customClient' ;
368
371
369
- async function _create ( config , app , client ) {
372
+ async function _create ( _config : any , _app : any , client : string ) {
370
373
if ( client !== clientName ) {
371
374
success = false ;
372
375
}
373
376
}
374
- const app = {
377
+ const app : any = {
375
378
config : {
376
379
dataService : {
377
380
clients : {
0 commit comments