Skip to content
Myr

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

2 files+123 added−1 removed

modified:src/middleware/timing/index.test.ts+120 added
101 unmodified lines
102 expect(consoleWarnSpy).not.toHaveBeenCalled()103 consoleWarnSpy.mockRestore()104 })105106 describe('Should handle timing options', async () => {107 it('Should handle autoEnd option', async () => {108 const autoEndApp = new Hono()109110 autoEndApp.use('*', timing())111 autoEndApp.get('/', (c) => {112 startTime(c, 'test')113 return c.text('/')114 })115116 const res = await autoEndApp.request('/')117118 expect(res.status).toBe(200)119 expect(res.headers.get('server-timing')).toContain('test;dur=')120121 const disabledAutoEndApp = new Hono()122123 disabledAutoEndApp.use('*', timing({ autoEnd: false }))124 disabledAutoEndApp.get('/', (c) => {125 startTime(c, 'test')126 return c.text('/')127 })128129 const disabledRes = await disabledAutoEndApp.request('/')130131 expect(disabledRes.status).toBe(200)132 expect(disabledRes.headers.get('server-timing')).not.toContain('test;dur=')133 })134135 it('Should use enabled function return value', async () => {136 const enabledApp = new Hono()137 const enabled = vi.fn(() => false)138139 enabledApp.use('*', timing({ enabled }))140 enabledApp.get('/', (c) => c.text('/'))141142 const res = await enabledApp.request('/')143144 expect(res.status).toBe(200)145 expect(enabled).toHaveBeenCalled()146 expect(res.headers.has('server-timing')).toBeFalsy()147 })148149 it('Should handle total false and value-less metrics without description', async () => {150 const metricApp = new Hono()151152 metricApp.use('*', timing({ total: false }))153 metricApp.get('/', (c) => {154 setMetric(c, 'test')155 return c.text('/')156 })157158 const res = await metricApp.request('/')159160 expect(res.status).toBe(200)161 expect(res.headers.get('server-timing')).toBe('test')162 })163 })164165 describe('Should handle crossOrigin setting', async () => {166 it('Should do nothing when crossOrigin is falsy', async () => {167 const crossOriginApp = new Hono()168
75 unmodified lines
244 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 })248249 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)254255 const app = new Hono().use(timing()).get('/', (c) => {256 startTime(c, 'test')257 endTime(c, 'test')258 return c.text('hello')259 })260261 const res = await app.request('/')262 expect(res.status).toBe(200)263264 expect(DateNowSpy).toHaveBeenCalled()265266 DateNowSpy.mockRestore()267 globalThis.performance = originalPerformance268 })269 })270271 describe('Configuration validation', async () => {272 it('Should silently warn when no timing metrics are available', async () => {273 const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})274275 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 })281282 const res = await app.request('/')283 expect(res.status).toBe(200)284285 expect(consoleWarnSpy).toHaveBeenNthCalledWith(286 2,287 'Metrics not initialized! Please add the `timing()` middleware to this route!'288 )289290 consoleWarnSpy.mockRestore()291 })292293 it('Should silently warn when trying to end a non-existent timer', async () => {294 const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})295296 const app = new Hono().use(timing()).get('/', (c) => {297 setMetric(c, 'region', 'europe-west3')298 endTime(c, 'nonExistentTimer')299 return c.text('hello')300 })301302 const res = await app.request('/')303 expect(res.status).toBe(200)304305 expect(consoleWarnSpy).toHaveBeenCalledWith('Timer "nonExistentTimer" does not exist!')306307 consoleWarnSpy.mockRestore()308 })309 })310})
modified:src/middleware/timing/timing.ts+3 added-1 removed
100 unmodified lines
101 endTime(c, 'total')102 }103104 if (options.autoEnd) {105 timers.forEach((_, key) => endTime(c, key))105 timers.forEach((_, key) => {106 endTime(c, key)107 })108 }109110 const enabled = typeof options.enabled === 'function' ? options.enabled(c) : options.enabled111
146 unmodified lines