Skip to content

Commit ce09c81

Browse files
committed
Update tests to use @sinonjs/fake-timer 15
1 parent 04cb773 commit ce09c81

15 files changed

+57
-69
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
"@fastify/busboy": "3.2.0",
113113
"@matteo.collina/tspl": "^0.2.0",
114114
"@metcoder95/https-pem": "^1.0.0",
115-
"@sinonjs/fake-timers": "^12.0.0",
115+
"@sinonjs/fake-timers": "^15.1.1",
116116
"@types/node": "^20.19.22",
117117
"abort-controller": "^3.0.0",
118118
"borp": "^0.20.0",

test/cache-interceptor/cache-store-test-utils.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { describe, test, after } = require('node:test')
55
const { Readable } = require('node:stream')
66
const { once } = require('node:events')
77
const FakeTimers = require('@sinonjs/fake-timers')
8+
const fakeTimersOpts = { toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval', 'setImmediate', 'clearImmediate', 'Date', 'hrtime', 'performance', 'Intl'] }
89

910
/**
1011
* @typedef {import('../../types/cache-interceptor.d.ts').default.CacheStore} CacheStore
@@ -109,9 +110,7 @@ function cacheStoreTests (CacheStore, options) {
109110
})
110111

111112
test('returns stale response before deleteAt', options, async () => {
112-
const clock = FakeTimers.install({
113-
shouldClearNativeTimers: true
114-
})
113+
const clock = FakeTimers.install({ ...fakeTimersOpts, shouldClearNativeTimers: true })
115114

116115
after(() => clock.uninstall())
117116

@@ -167,9 +166,7 @@ function cacheStoreTests (CacheStore, options) {
167166
})
168167

169168
test('a stale request is overwritten', options, async () => {
170-
const clock = FakeTimers.install({
171-
shouldClearNativeTimers: true
172-
})
169+
const clock = FakeTimers.install({ ...fakeTimersOpts, shouldClearNativeTimers: true })
173170

174171
after(() => clock.uninstall())
175172

test/client-keep-alive.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const { kConnect } = require('../lib/core/symbols')
88
const { createServer } = require('node:net')
99
const http = require('node:http')
1010
const FakeTimers = require('@sinonjs/fake-timers')
11+
const fakeTimersOpts = { toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval', 'setImmediate', 'clearImmediate', 'Date', 'hrtime', 'performance', 'Intl'] }
1112

1213
test('keep-alive header', async (t) => {
1314
t = tspl(t, { plan: 2 })
@@ -46,7 +47,7 @@ test('keep-alive header', async (t) => {
4647
test('keep-alive header 0', async (t) => {
4748
t = tspl(t, { plan: 2 })
4849

49-
const clock = FakeTimers.install()
50+
const clock = FakeTimers.install(fakeTimersOpts)
5051
after(() => clock.uninstall())
5152

5253
const server = createServer((socket) => {
@@ -151,7 +152,7 @@ test('keep-alive not timeout', async (t) => {
151152
t = tspl(t, { plan: 2 })
152153

153154
const clock = FakeTimers.install({
154-
apis: ['setTimeout']
155+
...fakeTimersOpts
155156
})
156157
after(() => clock.uninstall())
157158

@@ -193,7 +194,7 @@ test('keep-alive threshold', async (t) => {
193194
t = tspl(t, { plan: 2 })
194195

195196
const clock = FakeTimers.install({
196-
apis: ['setTimeout']
197+
...fakeTimersOpts
197198
})
198199
after(() => clock.uninstall())
199200

@@ -236,7 +237,7 @@ test('keep-alive max keepalive', async (t) => {
236237
t = tspl(t, { plan: 2 })
237238

238239
const clock = FakeTimers.install({
239-
apis: ['setTimeout']
240+
...fakeTimersOpts
240241
})
241242
after(() => clock.uninstall())
242243

test/client-reconnect.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ const { Client } = require('..')
77
const { createServer } = require('node:http')
88
const FakeTimers = require('@sinonjs/fake-timers')
99
const timers = require('../lib/util/timers')
10+
const fakeTimersOpts = { toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval', 'setImmediate', 'clearImmediate', 'Date', 'hrtime', 'performance', 'Intl'] }
1011

1112
test('multiple reconnect', async (t) => {
1213
t = tspl(t, { plan: 5 })
1314

1415
let n = 0
15-
const clock = FakeTimers.install()
16+
const clock = FakeTimers.install(fakeTimersOpts)
1617
after(() => clock.uninstall())
1718

1819
const orgTimers = { ...timers }

test/client-timeout.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const { createServer } = require('node:http')
77
const { Readable } = require('node:stream')
88
const FakeTimers = require('@sinonjs/fake-timers')
99
const timers = require('../lib/util/timers')
10+
const fakeTimersOpts = { toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval', 'setImmediate', 'clearImmediate', 'Date', 'hrtime', 'performance', 'Intl'] }
1011

1112
test('refresh timeout on pause', async (t) => {
1213
t = tspl(t, { plan: 1 })
@@ -52,7 +53,7 @@ test('refresh timeout on pause', async (t) => {
5253
test('start headers timeout after request body', async (t) => {
5354
t = tspl(t, { plan: 2 })
5455

55-
const clock = FakeTimers.install({ shouldClearNativeTimers: true })
56+
const clock = FakeTimers.install({ ...fakeTimersOpts, shouldClearNativeTimers: true })
5657
after(() => clock.uninstall())
5758

5859
const orgTimers = { ...timers }
@@ -110,7 +111,7 @@ test('start headers timeout after request body', async (t) => {
110111
test('start headers timeout after async iterator request body', async (t) => {
111112
t = tspl(t, { plan: 1 })
112113

113-
const clock = FakeTimers.install({ shouldClearNativeTimers: true })
114+
const clock = FakeTimers.install({ ...fakeTimersOpts, shouldClearNativeTimers: true })
114115
after(() => clock.uninstall())
115116

116117
const orgTimers = { ...timers }

test/eventsource/eventsource-connect.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const http = require('node:http')
55
const { test, describe, after } = require('node:test')
66
const FakeTimers = require('@sinonjs/fake-timers')
77
const { EventSource, defaultReconnectionTime } = require('../../lib/web/eventsource/eventsource')
8+
const fakeTimersOpts = { toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval', 'setImmediate', 'clearImmediate', 'Date', 'hrtime', 'performance', 'Intl'] }
89
const { randomInt } = require('node:crypto')
910

1011
describe('EventSource - sending correct request headers', () => {
@@ -176,7 +177,7 @@ describe('EventSource - received response must have content-type to be text/even
176177
})
177178

178179
test('should try to connect again if server is unreachable', async (t) => {
179-
const clock = FakeTimers.install()
180+
const clock = FakeTimers.install(fakeTimersOpts)
180181

181182
after(() => clock.uninstall())
182183
const reconnectionTime = defaultReconnectionTime
@@ -209,7 +210,7 @@ describe('EventSource - received response must have content-type to be text/even
209210

210211
test('should try to connect again if server is unreachable, configure reconnectionTime', async (t) => {
211212
const reconnectionTime = 1000
212-
const clock = FakeTimers.install()
213+
const clock = FakeTimers.install(fakeTimersOpts)
213214
after(() => clock.uninstall())
214215

215216
const domain = 'bad.n' + randomInt(1e10).toString(36) + '.proxy'

test/eventsource/eventsource-message.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const http = require('node:http')
55
const { test, describe, after } = require('node:test')
66
const { EventSource, defaultReconnectionTime } = require('../../lib/web/eventsource/eventsource')
77
const FakeTimers = require('@sinonjs/fake-timers')
8+
const fakeTimersOpts = { toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval', 'setImmediate', 'clearImmediate', 'Date', 'hrtime', 'performance', 'Intl'] }
89

910
describe('EventSource - message', () => {
1011
test('Should not emit a message if only retry field was sent', (t, done) => {
@@ -202,7 +203,7 @@ describe('EventSource - message', () => {
202203
})
203204

204205
test('Should not emit a custom type message if no data is provided', (t, done) => {
205-
const clock = FakeTimers.install()
206+
const clock = FakeTimers.install(fakeTimersOpts)
206207
after(() => clock.uninstall())
207208

208209
t.plan(1)

test/eventsource/eventsource-reconnect.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ const http = require('node:http')
55
const { test, describe, after } = require('node:test')
66
const FakeTimers = require('@sinonjs/fake-timers')
77
const { EventSource, defaultReconnectionTime } = require('../../lib/web/eventsource/eventsource')
8+
const fakeTimersOpts = { toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval', 'setImmediate', 'clearImmediate', 'Date', 'hrtime', 'performance', 'Intl'] }
89

910
describe('EventSource - reconnect', () => {
1011
test('Should reconnect on connection closed by server', (t, done) => {
1112
t.plan(1)
1213

13-
const clock = FakeTimers.install()
14+
const clock = FakeTimers.install(fakeTimersOpts)
1415
after(() => clock.uninstall())
1516

1617
const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => {
@@ -43,7 +44,7 @@ describe('EventSource - reconnect', () => {
4344

4445
test('Should reconnect on with reconnection timeout', (t, done) => {
4546
t.plan(2)
46-
const clock = FakeTimers.install()
47+
const clock = FakeTimers.install(fakeTimersOpts)
4748
after(() => clock.uninstall())
4849

4950
const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => {
@@ -80,7 +81,7 @@ describe('EventSource - reconnect', () => {
8081

8182
test('Should reconnect on with modified reconnection timeout', (t, done) => {
8283
t.plan(3)
83-
const clock = FakeTimers.install()
84+
const clock = FakeTimers.install(fakeTimersOpts)
8485
after(() => clock.uninstall())
8586

8687
const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => {
@@ -119,7 +120,7 @@ describe('EventSource - reconnect', () => {
119120

120121
test('Should reconnect and send lastEventId', async (t) => {
121122
t.plan(1)
122-
const clock = FakeTimers.install()
123+
const clock = FakeTimers.install(fakeTimersOpts)
123124
after(() => clock.uninstall())
124125

125126
let requestCount = 0

test/fetch/fetch-timeouts.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ const timers = require('../../lib/util/timers')
77
const { createServer } = require('node:http')
88
const FakeTimers = require('@sinonjs/fake-timers')
99
const { closeServerAsPromise } = require('../utils/node-http')
10+
const fakeTimersOpts = { toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval', 'setImmediate', 'clearImmediate', 'Date', 'hrtime', 'performance', 'Intl'] }
1011

1112
test('Fetch very long request, timeout overridden so no error', (t, done) => {
1213
const minutes = 6
1314
const msToDelay = 1000 * 60 * minutes
1415

1516
t.plan(1)
1617

17-
const clock = FakeTimers.install()
18+
const clock = FakeTimers.install(fakeTimersOpts)
1819
t.after(clock.uninstall.bind(clock))
1920

2021
const orgTimers = { ...timers }

test/interceptors/cache-async-store.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const { request, Client, interceptors } = require('../../index')
99
const MemoryCacheStore = require('../../lib/cache/memory-cache-store')
1010
const FakeTimers = require('@sinonjs/fake-timers')
1111
const { setTimeout } = require('node:timers/promises')
12+
const fakeTimersOpts = { toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval', 'setImmediate', 'clearImmediate', 'Date', 'hrtime', 'performance', 'Intl'] }
1213

1314
/**
1415
* Wraps a MemoryCacheStore to simulate an async remote store:
@@ -49,7 +50,7 @@ class AsyncCacheStore {
4950

5051
describe('cache interceptor with async store', () => {
5152
test('stale-while-revalidate 304 refreshes cache with async store', async () => {
52-
const clock = FakeTimers.install({ now: 1 })
53+
const clock = FakeTimers.install({ ...fakeTimersOpts, now: 1 })
5354
after(() => clock.uninstall())
5455

5556
let count200 = 0
@@ -118,7 +119,7 @@ describe('cache interceptor with async store', () => {
118119
})
119120

120121
test('stale-while-revalidate 200 refreshes cache with async store', async () => {
121-
const clock = FakeTimers.install({ now: 1 })
122+
const clock = FakeTimers.install({ ...fakeTimersOpts, now: 1 })
122123
after(() => clock.uninstall())
123124

124125
let requestCount = 0
@@ -172,7 +173,7 @@ describe('cache interceptor with async store', () => {
172173
})
173174

174175
test('null vary values are not sent in revalidation headers', async () => {
175-
const clock = FakeTimers.install({ now: 1 })
176+
const clock = FakeTimers.install({ ...fakeTimersOpts, now: 1 })
176177
after(() => clock.uninstall())
177178

178179
let revalidationHeaders = null

0 commit comments

Comments
 (0)