11'use strict'
22
3- const assert = require ( 'node:assert' )
43const { once } = require ( 'node:events' )
54const http = require ( 'node:http' )
6- const { test, describe } = require ( 'node:test' )
5+ const { test, describe, before , after } = require ( 'node:test' )
76const { EventSource } = require ( '../../lib/web/eventsource/eventsource' )
87
9- describe ( 'EventSource - eventhandler idl' , async ( ) => {
10- const server = http . createServer ( { joinDuplicateHeaders : true } , ( req , res ) => {
11- res . writeHead ( 200 , 'dummy' )
8+ describe ( 'EventSource - eventhandler idl' , ( ) => {
9+ let server
10+ let port
11+
12+ before ( async ( ) => {
13+ server = http . createServer ( { joinDuplicateHeaders : true } , ( req , res ) => {
14+ res . writeHead ( 200 , 'dummy' )
15+ } )
16+
17+ await once ( server . listen ( 0 ) , 'listening' )
18+ port = server . address ( ) . port
1219 } )
1320
14- await once ( server . listen ( 0 ) , 'listening' )
15- const port = server . address ( ) . port
21+ after ( ( ) => { server . close ( ) } )
1622
17- let done = 0
1823 const eventhandlerIdl = [ 'onmessage' , 'onerror' , 'onopen' ]
1924
2025 eventhandlerIdl . forEach ( ( type ) => {
21- test ( `Should properly configure the ${ type } eventhandler idl` , ( ) => {
26+ test ( `Should properly configure the ${ type } eventhandler idl` , ( t ) => {
2227 const eventSourceInstance = new EventSource ( `http://localhost:${ port } ` )
2328
2429 // Eventsource eventhandler idl is by default null,
25- assert . strictEqual ( eventSourceInstance [ type ] , null )
30+ t . assert . strictEqual ( eventSourceInstance [ type ] , null )
2631
2732 // The eventhandler idl is by default not enumerable.
28- assert . strictEqual ( Object . prototype . propertyIsEnumerable . call ( eventSourceInstance , type ) , false )
33+ t . assert . strictEqual ( Object . prototype . propertyIsEnumerable . call ( eventSourceInstance , type ) , false )
2934
3035 // The eventhandler idl ignores non-functions.
3136 eventSourceInstance [ type ] = 7
32- assert . strictEqual ( EventSource [ type ] , undefined )
37+ t . assert . strictEqual ( EventSource [ type ] , undefined )
3338
3439 // The eventhandler idl accepts functions.
3540 function fn ( ) {
36- assert . fail ( 'Should not have called the eventhandler' )
41+ t . assert . fail ( 'Should not have called the eventhandler' )
3742 }
3843 eventSourceInstance [ type ] = fn
39- assert . strictEqual ( eventSourceInstance [ type ] , fn )
44+ t . assert . strictEqual ( eventSourceInstance [ type ] , fn )
4045
4146 // The eventhandler idl can be set to another function.
4247 function fn2 ( ) { }
4348 eventSourceInstance [ type ] = fn2
44- assert . strictEqual ( eventSourceInstance [ type ] , fn2 )
49+ t . assert . strictEqual ( eventSourceInstance [ type ] , fn2 )
4550
4651 // The eventhandler idl overrides the previous function.
4752 eventSourceInstance . dispatchEvent ( new Event ( type ) )
4853
4954 eventSourceInstance . close ( )
50- done ++
51-
52- if ( done === eventhandlerIdl . length ) server . close ( )
5355 } )
5456 } )
5557} )
@@ -60,31 +62,31 @@ describe('EventSource - constants', () => {
6062 [ 'OPEN' , 1 ] ,
6163 [ 'CLOSED' , 2 ]
6264 ] . forEach ( ( config ) => {
63- test ( `Should expose the ${ config [ 0 ] } constant` , ( ) => {
65+ test ( `Should expose the ${ config [ 0 ] } constant` , ( t ) => {
6466 const [ constant , value ] = config
6567
6668 // EventSource exposes the constant.
67- assert . strictEqual ( Object . hasOwn ( EventSource , constant ) , true )
69+ t . assert . strictEqual ( Object . hasOwn ( EventSource , constant ) , true )
6870
6971 // The value is properly set.
70- assert . strictEqual ( EventSource [ constant ] , value )
72+ t . assert . strictEqual ( EventSource [ constant ] , value )
7173
7274 // The constant is enumerable.
73- assert . strictEqual ( Object . prototype . propertyIsEnumerable . call ( EventSource , constant ) , true )
75+ t . assert . strictEqual ( Object . prototype . propertyIsEnumerable . call ( EventSource , constant ) , true )
7476
7577 // The constant is not writable.
7678 try {
7779 EventSource [ constant ] = 666
7880 } catch ( e ) {
79- assert . strictEqual ( e instanceof TypeError , true )
81+ t . assert . strictEqual ( e instanceof TypeError , true )
8082 }
8183 // The constant is not configurable.
8284 try {
8385 delete EventSource [ constant ]
8486 } catch ( e ) {
85- assert . strictEqual ( e instanceof TypeError , true )
87+ t . assert . strictEqual ( e instanceof TypeError , true )
8688 }
87- assert . strictEqual ( EventSource [ constant ] , value )
89+ t . assert . strictEqual ( EventSource [ constant ] , value )
8890 } )
8991 } )
9092} )
0 commit comments