refactor(timing): Test/add test for middleware timing (#4991)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* test(timing): add some test for coverage * refactor(timing): prevent return in forEach
authored by Valère · committed by GitHub · parent d743bb7 · Browse files · 2 files·+123 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+123 added−1 removed
- tests:+120 added−0 removed
- other:+3 added−1 removed
Provenance
- Commit
5ef800e- Tree
fefbe6d- Author
Valère <pique.valere@gmx.fr>- Committer
GitHub <noreply@github.com>- Parent
d743bb7
2 files changed+123 added−1 removed
modified:src/middleware/timing/index.test.ts+120 added
101 unmodified lines102 expect(consoleWarnSpy).not.toHaveBeenCalled()103 consoleWarnSpy.mockRestore()104 })105 106+ describe('Should handle timing options', async () => {107+ it('Should handle autoEnd option', async () => {108+ const autoEndApp = new Hono()109+110+ autoEndApp.use('*', timing())111+ autoEndApp.get('/', (c) => {112+ startTime(c, 'test')113+ return c.text('/')114+ })115+116+ const res = await autoEndApp.request('/')117+118+ expect(res.status).toBe(200)119+ expect(res.headers.get('server-timing')).toContain('test;dur=')120+121+ const disabledAutoEndApp = new Hono()122+123+ disabledAutoEndApp.use('*', timing({ autoEnd: false }))124+ disabledAutoEndApp.get('/', (c) => {125+ startTime(c, 'test')126+ return c.text('/')127+ })128+129+ const disabledRes = await disabledAutoEndApp.request('/')130+131+ expect(disabledRes.status).toBe(200)132+ expect(disabledRes.headers.get('server-timing')).not.toContain('test;dur=')133+ })134+135+ it('Should use enabled function return value', async () => {136+ const enabledApp = new Hono()137+ const enabled = vi.fn(() => false)138+139+ enabledApp.use('*', timing({ enabled }))140+ enabledApp.get('/', (c) => c.text('/'))141+142+ const res = await enabledApp.request('/')143+144+ expect(res.status).toBe(200)145+ expect(enabled).toHaveBeenCalled()146+ expect(res.headers.has('server-timing')).toBeFalsy()147+ })148+149+ it('Should handle total false and value-less metrics without description', async () => {150+ const metricApp = new Hono()151+152+ metricApp.use('*', timing({ total: false }))153+ metricApp.get('/', (c) => {154+ setMetric(c, 'test')155+ return c.text('/')156+ })157+158+ const res = await metricApp.request('/')159+160+ expect(res.status).toBe(200)161+ expect(res.headers.get('server-timing')).toBe('test')162+ })163+ })164+165 describe('Should handle crossOrigin setting', async () => {166 it('Should do nothing when crossOrigin is falsy', async () => {167 const crossOriginApp = new Hono()168 75 unmodified lines244 expect(res.headers.has('server-timing')).toBeTruthy()245 expect(res.headers.has('timing-allow-origin')).toBeTruthy()246 expect(res.headers.get('timing-allow-origin')).toBe('https://example.com')247 })248+249+ it("Should use Date.now as backup if performance API isn't available", async () => {250+ const originalPerformance = globalThis.performance251+ // @ts-expect-error disable performance API for testing252+ delete globalThis.performance253+ const DateNowSpy = vi.spyOn(Date, 'now').mockReturnValue(123456789)254+255+ const app = new Hono().use(timing()).get('/', (c) => {256+ startTime(c, 'test')257+ endTime(c, 'test')258+ return c.text('hello')259+ })260+261+ const res = await app.request('/')262+ expect(res.status).toBe(200)263+264+ expect(DateNowSpy).toHaveBeenCalled()265+266+ DateNowSpy.mockRestore()267+ globalThis.performance = originalPerformance268+ })269 })270+271+ describe('Configuration validation', async () => {272+ it('Should silently warn when no timing metrics are available', async () => {273+ const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})274+275+ const app = new Hono().get('/', (c) => {276+ setMetric(c, 'test', 123)277+ startTime(c, 'test')278+ endTime(c, 'test')279+ return c.text('hello')280+ })281+282+ const res = await app.request('/')283+ expect(res.status).toBe(200)284+285+ expect(consoleWarnSpy).toHaveBeenNthCalledWith(286+ 2,287+ 'Metrics not initialized! Please add the `timing()` middleware to this route!'288+ )289+290+ consoleWarnSpy.mockRestore()291+ })292+293+ it('Should silently warn when trying to end a non-existent timer', async () => {294+ const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})295+296+ const app = new Hono().use(timing()).get('/', (c) => {297+ setMetric(c, 'region', 'europe-west3')298+ endTime(c, 'nonExistentTimer')299+ return c.text('hello')300+ })301+302+ const res = await app.request('/')303+ expect(res.status).toBe(200)304+305+ expect(consoleWarnSpy).toHaveBeenCalledWith('Timer "nonExistentTimer" does not exist!')306+307+ consoleWarnSpy.mockRestore()308+ })309+ })310 })modified:src/middleware/timing/timing.ts+3 added-1 removed
100 unmodified lines101 endTime(c, 'total')102 }103 104 if (options.autoEnd) {105- timers.forEach((_, key) => endTime(c, key))105+ timers.forEach((_, key) => {106+ endTime(c, key)107+ })108 }109 110 const enabled = typeof options.enabled === 'function' ? options.enabled(c) : options.enabled111 146 unmodified lines