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+
45class DataService {
5- constructor ( config ) {
6+ config : any ;
7+ constructor ( config : any ) {
68 this . config = config ;
79 }
810
@@ -11,41 +13,39 @@ class DataService {
1113 }
1214}
1315
14- function create ( config ) {
16+ function create ( config : any ) {
1517 return new DataService ( config ) ;
1618}
1719
18- async function asyncCreate ( config ) {
19- await sleep ( 10 ) ;
20+ async function asyncCreate ( config : any ) {
21+ await scheduler . wait ( 10 ) ;
2022 return new DataService ( config ) ;
2123}
2224
23- describe ( 'test/lib/core/singleton.test.js' , ( ) => {
24-
25+ describe ( 'test/lib/core/singleton.test.ts' , ( ) => {
2526 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 ;
2829 } ) ;
2930
3031 describe ( 'sync singleton creation tests' , ( ) => {
31-
3232 it ( 'should init with client' , async ( ) => {
3333 const name = 'dataService' ;
3434
3535 const clients = [
3636 { foo : 'bar' } ,
3737 ] ;
3838 for ( const client of clients ) {
39- const app = { config : { dataService : { client } } } ;
39+ const app : any = { config : { dataService : { client } } } ;
4040 const singleton = new Singleton ( {
4141 name,
4242 app,
4343 create,
4444 } ) ;
4545 singleton . init ( ) ;
4646 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' ) ;
4949 }
5050 } ) ;
5151
@@ -57,21 +57,21 @@ describe('test/lib/core/singleton.test.js', () => {
5757 second : { foo : 'bar2' } ,
5858 } ;
5959
60- const app = { config : { dataService : { clients } } } ;
60+ const app : any = { config : { dataService : { clients } } } ;
6161 const singleton = new Singleton ( {
6262 name,
6363 app,
6464 create,
6565 } ) ;
6666 singleton . init ( ) ;
6767 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' ) ;
7171 } ) ;
7272
7373 it ( 'should client support default' , async ( ) => {
74- const app = {
74+ const app : any = {
7575 config : {
7676 dataService : {
7777 client : { foo : 'bar' } ,
@@ -88,13 +88,13 @@ describe('test/lib/core/singleton.test.js', () => {
8888 } ) ;
8989 singleton . init ( ) ;
9090 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' ) ;
9494 } ) ;
9595
9696 it ( 'should clients support default' , async ( ) => {
97- const app = {
97+ const app : any = {
9898 config : {
9999 dataService : {
100100 clients : {
@@ -124,7 +124,7 @@ describe('test/lib/core/singleton.test.js', () => {
124124 } ) ;
125125
126126 it ( 'should createInstance without client/clients support default' , async ( ) => {
127- const app = {
127+ const app : any = {
128128 config : {
129129 dataService : {
130130 default : { foo : 'bar' } ,
@@ -148,12 +148,12 @@ describe('test/lib/core/singleton.test.js', () => {
148148 } ) ;
149149
150150 it ( 'should work with unextensible' , async ( ) => {
151- function create ( config ) {
151+ function create ( config : any ) {
152152 const d = new DataService ( config ) ;
153153 Object . preventExtensions ( d ) ;
154154 return d ;
155155 }
156- const app = {
156+ const app : any = {
157157 config : {
158158 dataService : {
159159 client : { foo : 'bar' } ,
@@ -176,12 +176,12 @@ describe('test/lib/core/singleton.test.js', () => {
176176 } ) ;
177177
178178 it ( 'should work with frozen' , async ( ) => {
179- function create ( config ) {
179+ function create ( config : any ) {
180180 const d = new DataService ( config ) ;
181181 Object . freeze ( d ) ;
182182 return d ;
183183 }
184- const app = {
184+ const app : any = {
185185 config : {
186186 dataService : {
187187 client : { foo : 'bar' } ,
@@ -211,17 +211,19 @@ describe('test/lib/core/singleton.test.js', () => {
211211 Object . freeze ( d ) ;
212212 return d ;
213213 }
214- const app = {
214+ const app : any = {
215215 config : {
216216 dataService : {
217217 client : { foo : 'bar' } ,
218218 default : { foo : 'bar' } ,
219219 } ,
220220 } ,
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+ }
225227 } ,
226228 } ,
227229 } ;
@@ -243,12 +245,12 @@ describe('test/lib/core/singleton.test.js', () => {
243245 let success = true ;
244246 const name = 'dataService' ;
245247 const clientName = 'customClient' ;
246- function create ( config , app , client ) {
248+ function create ( _config : any , _app : any , client : string ) {
247249 if ( client !== clientName ) {
248250 success = false ;
249251 }
250252 }
251- const app = {
253+ const app : any = {
252254 config : {
253255 dataService : {
254256 clients : {
@@ -276,7 +278,7 @@ describe('test/lib/core/singleton.test.js', () => {
276278 { foo : 'bar' } ,
277279 ] ;
278280 for ( const client of clients ) {
279- const app = { config : { dataService : { client } } } ;
281+ const app : any = { config : { dataService : { client } } } ;
280282 const singleton = new Singleton ( {
281283 name,
282284 app,
@@ -298,7 +300,7 @@ describe('test/lib/core/singleton.test.js', () => {
298300 second : { foo : 'bar2' } ,
299301 } ;
300302
301- const app = { config : { dataService : { clients } } } ;
303+ const app : any = { config : { dataService : { clients } } } ;
302304 const singleton = new Singleton ( {
303305 name,
304306 app,
@@ -312,7 +314,7 @@ describe('test/lib/core/singleton.test.js', () => {
312314 } ) ;
313315
314316 it ( 'should createInstanceAsync without client/clients support default' , async ( ) => {
315- const app = {
317+ const app : any = {
316318 config : {
317319 dataService : {
318320 default : { foo : 'bar' } ,
@@ -336,7 +338,7 @@ describe('test/lib/core/singleton.test.js', () => {
336338 } ) ;
337339
338340 it ( 'should createInstanceAsync throw error' , async ( ) => {
339- const app = {
341+ const app : any = {
340342 config : {
341343 dataService : {
342344 default : { foo : 'bar' } ,
@@ -356,8 +358,9 @@ describe('test/lib/core/singleton.test.js', () => {
356358 try {
357359 app . dataService = await app . dataService . createInstance ( { foo1 : 'bar1' } ) ;
358360 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' ) ;
361364 }
362365 } ) ;
363366
@@ -366,12 +369,12 @@ describe('test/lib/core/singleton.test.js', () => {
366369 const name = 'dataService' ;
367370 const clientName = 'customClient' ;
368371
369- async function _create ( config , app , client ) {
372+ async function _create ( _config : any , _app : any , client : string ) {
370373 if ( client !== clientName ) {
371374 success = false ;
372375 }
373376 }
374- const app = {
377+ const app : any = {
375378 config : {
376379 dataService : {
377380 clients : {
0 commit comments