honojs-middleware/test/index.test.ts

43 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-08-04 07:54:30 +08:00
import { Hono } from 'hono'
2022-08-04 07:33:14 +08:00
import { sentry } from '../src'
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.')
}
async waitUntil(promise: Promise<any>): Promise<void> {
await promise
}
}
const captureException = jest.fn()
jest.mock('toucan-js', () => jest.fn().mockImplementation(() => ({ captureException })))
2022-08-13 03:33:42 +08:00
const callback = jest.fn()
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))
app.get('/sentry/foo', (c) => c.text('foo'))
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
})
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())
expect(res).not.toBeNull()
expect(res.status).toBe(500)
expect(captureException).toHaveBeenCalled()
})
2022-08-04 07:54:30 +08:00
})