@@ -6,6 +6,27 @@ import {
6
6
fail ,
7
7
} from "https://deno.land/std@0.127.0/testing/asserts.ts" ;
8
8
9
+ const waitForTimeout = (
10
+ fn : ( args ?: unknown [ ] ) => void | Promise < void > ,
11
+ timeout : number ,
12
+ // deno-lint-ignore no-explicit-any
13
+ ...args : any [ ]
14
+ ) : Promise < void > => {
15
+ return new Promise ( ( resolve ) => {
16
+ const timeoutId = setTimeout (
17
+ async ( args ) => {
18
+ await fn ( args ) ;
19
+
20
+ clearTimeout ( timeoutId ) ;
21
+
22
+ resolve ( ) ;
23
+ } ,
24
+ timeout ,
25
+ ...args ,
26
+ ) ;
27
+ } ) ;
28
+ } ;
29
+
9
30
Deno . test ( "EventEmitter" , async ( ctx ) => {
10
31
type ReservedEvents = {
11
32
reserved : string ;
@@ -21,12 +42,14 @@ Deno.test("EventEmitter", async (ctx) => {
21
42
constructor ( ) {
22
43
super ( ) ;
23
44
24
- const timeout = setTimeout ( ( ) => {
45
+ this . run ( ) ;
46
+ }
47
+
48
+ protected async run ( ) {
49
+ await waitForTimeout ( ( ) => {
25
50
this . emitReserved ( "reserved" , "reserved" ) ;
26
51
27
52
this . emitReserved ( "reserved2" , [ "reserved2" ] ) ;
28
-
29
- clearTimeout ( timeout ) ;
30
53
} , 10 ) ;
31
54
}
32
55
}
@@ -193,36 +216,32 @@ Deno.test("EventEmitter base functions", async (ctx) => {
193
216
} ) ;
194
217
195
218
await ctx . step ( "pull" , async ( ctx ) => {
196
- await ctx . step ( "without timeout" , ( ) => {
219
+ await ctx . step ( "without timeout" , async ( ) => {
197
220
const target = new EventEmitter < Events > ( ) ;
198
221
199
222
const promise = target . pull ( "foo" ) ;
200
223
201
- const timeout = setTimeout ( async ( ) => {
224
+ await waitForTimeout ( async ( ) => {
202
225
target . emit ( "foo" , "bar" ) ;
203
226
204
227
const detail = await promise ;
205
228
206
229
assertStrictEquals ( detail , "bar" ) ;
207
-
208
- clearTimeout ( timeout ) ;
209
230
} , 10 ) ;
210
231
} ) ;
211
232
212
233
await ctx . step ( "with timeout" , async ( ctx ) => {
213
- await ctx . step ( "should resolve" , ( ) => {
234
+ await ctx . step ( "should resolve" , async ( ) => {
214
235
const target = new EventEmitter < Events > ( ) ;
215
236
216
237
const promise = target . pull ( "foo" , 20 ) ;
217
238
218
- const timeout = setTimeout ( async ( ) => {
239
+ await waitForTimeout ( async ( ) => {
219
240
target . emit ( "foo" , "bar" ) ;
220
241
221
242
const detail = await promise ;
222
243
223
244
assertStrictEquals ( detail , "bar" ) ;
224
-
225
- clearTimeout ( timeout ) ;
226
245
} , 10 ) ;
227
246
} ) ;
228
247
0 commit comments