2022-08-04 07:54:30 +08:00
|
|
|
import { Hono } from 'hono'
|
2025-03-04 21:00:28 +08:00
|
|
|
import { Toucan } from 'toucan-js'
|
|
|
|
import { getSentry, sentry } from '.'
|
2022-08-04 07:54:30 +08:00
|
|
|
|
2022-08-11 23:37:53 +08:00
|
|
|
// Mock
|
|
|
|
class Context implements ExecutionContext {
|
|
|
|
passThroughOnException(): void {
|
|
|
|
throw new Error('Method not implemented.')
|
|
|
|
}
|
2023-08-23 09:57:54 +08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2022-08-11 23:37:53 +08:00
|
|
|
async waitUntil(promise: Promise<any>): Promise<void> {
|
|
|
|
await promise
|
|
|
|
}
|
2025-03-29 08:12:39 +08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
props: any
|
2022-08-11 23:37:53 +08:00
|
|
|
}
|
|
|
|
|
2025-03-04 21:00:28 +08:00
|
|
|
vi.mock(import('toucan-js'), async (importOriginal) => {
|
|
|
|
const original = await importOriginal()
|
2023-08-24 10:21:16 +08:00
|
|
|
|
2025-03-04 21:00:28 +08:00
|
|
|
Object.assign(original.Toucan.prototype, { captureException: vi.fn(), log: vi.fn() })
|
2023-08-24 10:21:16 +08:00
|
|
|
|
2025-03-04 21:00:28 +08:00
|
|
|
return original
|
|
|
|
})
|
|
|
|
|
|
|
|
const callback = vi.fn()
|
2022-08-09 23:49:41 +08:00
|
|
|
|
2022-08-04 07:33:14 +08:00
|
|
|
describe('Sentry middleware', () => {
|
2022-08-04 07:54:30 +08:00
|
|
|
const app = new Hono()
|
|
|
|
|
2022-08-13 03:33:42 +08:00
|
|
|
app.use('/sentry/*', sentry(undefined, callback))
|
2022-08-09 23:49:41 +08:00
|
|
|
app.get('/sentry/foo', (c) => c.text('foo'))
|
2022-10-09 23:57:51 +08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
2023-08-24 10:21:16 +08:00
|
|
|
// @ts-expect-error
|
2022-09-15 05:02:03 +08:00
|
|
|
app.get('/sentry/bar', (c) => getSentry(c).log('bar') || c.text('bar'))
|
2022-08-09 23:49:41 +08:00
|
|
|
app.get('/sentry/error', () => {
|
|
|
|
throw new Error('a catastrophic error')
|
|
|
|
})
|
2022-08-04 07:54:30 +08:00
|
|
|
|
2022-08-04 07:33:14 +08:00
|
|
|
it('Should initialize Toucan', async () => {
|
2022-08-11 23:37:53 +08:00
|
|
|
const req = new Request('http://localhost/sentry/foo')
|
|
|
|
const res = await app.fetch(req, {}, new Context())
|
2022-08-04 07:54:30 +08:00
|
|
|
expect(res).not.toBeNull()
|
|
|
|
expect(res.status).toBe(200)
|
2022-08-13 03:33:42 +08:00
|
|
|
expect(callback).toHaveBeenCalled()
|
2022-08-04 07:54:30 +08:00
|
|
|
})
|
2022-08-09 23:49:41 +08:00
|
|
|
|
2022-09-03 01:11:34 +08:00
|
|
|
it('Should make Sentry available via context', async () => {
|
|
|
|
const req = new Request('http://localhost/sentry/bar')
|
|
|
|
const res = await app.fetch(req, {}, new Context())
|
|
|
|
expect(res).not.toBeNull()
|
2022-09-15 05:02:03 +08:00
|
|
|
expect(res.status).toBe(200)
|
2025-03-04 21:00:28 +08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-expect-error
|
|
|
|
expect(Toucan.prototype.log).toHaveBeenCalled()
|
2022-09-03 01:11:34 +08:00
|
|
|
})
|
|
|
|
|
2022-08-09 23:49:41 +08:00
|
|
|
it('Should report errors', async () => {
|
2022-08-11 23:37:53 +08:00
|
|
|
const req = new Request('http://localhost/sentry/error')
|
|
|
|
const res = await app.fetch(req, {}, new Context())
|
2022-08-09 23:49:41 +08:00
|
|
|
expect(res).not.toBeNull()
|
|
|
|
expect(res.status).toBe(500)
|
2025-03-04 21:00:28 +08:00
|
|
|
expect(Toucan.prototype.captureException).toHaveBeenCalled()
|
2022-08-09 23:49:41 +08:00
|
|
|
})
|
2022-08-04 07:54:30 +08:00
|
|
|
})
|