Skip to content

Commit 3c93bd4

Browse files
authored
chore: remove tspl from eventsource (#4569)
1 parent 26e85d1 commit 3c93bd4

17 files changed

+594
-599
lines changed
Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,57 @@
11
'use strict'
22

3-
const assert = require('node:assert')
43
const { once } = require('node:events')
54
const http = require('node:http')
6-
const { test, describe } = require('node:test')
5+
const { test, describe, before, after } = require('node:test')
76
const { 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
})
Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
'use strict'
22

3-
const { tspl } = require('@matteo.collina/tspl')
43
const { once } = require('node:events')
54
const http = require('node:http')
65
const { test, describe, after } = require('node:test')
76
const { EventSource } = require('../../lib/web/eventsource/eventsource')
87

98
describe('EventSource - close', () => {
109
test('should not emit error when closing the EventSource Instance', async (t) => {
11-
t = tspl(t, { plan: 1 })
10+
t.plan(1)
1211

1312
const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => {
14-
t.strictEqual(req.headers.connection, 'keep-alive')
13+
t.assert.strictEqual(req.headers.connection, 'keep-alive')
1514
res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' })
1615
res.write('data: hello\n\n')
1716

@@ -29,35 +28,34 @@ describe('EventSource - close', () => {
2928
}
3029

3130
eventSourceInstance.onerror = () => {
32-
t.fail('Should not have errored')
31+
t.assert.fail('Should not have errored')
3332
}
3433

35-
await t.completed
34+
await once(server, 'close')
3635
})
3736

3837
test('should set readyState to CLOSED', async (t) => {
39-
t = tspl(t, { plan: 3 })
38+
t.plan(3)
4039
const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => {
41-
t.strictEqual(req.headers.connection, 'keep-alive')
40+
t.assert.strictEqual(req.headers.connection, 'keep-alive')
4241
res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' })
4342
res.write('data: hello\n\n')
4443
})
45-
4644
after(() => server.close())
4745
await once(server.listen(0), 'listening')
4846
const port = server.address().port
4947

5048
const eventSourceInstance = new EventSource(`http://localhost:${port}`)
5149
eventSourceInstance.onopen = () => {
52-
t.strictEqual(eventSourceInstance.readyState, EventSource.OPEN)
50+
t.assert.strictEqual(eventSourceInstance.readyState, EventSource.OPEN)
5351
eventSourceInstance.close()
54-
t.strictEqual(eventSourceInstance.readyState, EventSource.CLOSED)
52+
t.assert.strictEqual(eventSourceInstance.readyState, EventSource.CLOSED)
53+
server.close()
5554
}
5655

5756
eventSourceInstance.onerror = () => {
58-
t.fail('Should not have errored')
57+
t.assert.fail('Should not have errored')
5958
}
60-
61-
await t.completed
59+
await once(server, 'close')
6260
})
6361
})

0 commit comments

Comments
 (0)