From ca0954a3103b7d45dc6baa4b9ea348c930ffb121 Mon Sep 17 00:00:00 2001 From: Samuel Lippert Date: Tue, 9 Aug 2022 10:49:41 -0500 Subject: [PATCH] Mock Toucan module, check captureException call --- test/index.test.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/test/index.test.ts b/test/index.test.ts index c25665a3..07272036 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,15 +1,28 @@ import { Hono } from 'hono' import { sentry } from '../src' +const captureException = jest.fn() +jest.mock('toucan-js', () => jest.fn().mockImplementation(() => ({ captureException }))) + describe('Sentry middleware', () => { const app = new Hono() - app.use('/hello/*', sentry()) - app.get('/hello/foo', (c) => c.text('foo')) + app.use('/sentry/*', sentry()) + app.get('/sentry/foo', (c) => c.text('foo')) + app.get('/sentry/error', () => { + throw new Error('a catastrophic error') + }) it('Should initialize Toucan', async () => { - const res = await app.request('http://localhost/hello/foo') + const res = await app.request('http://localhost/sentry/foo') expect(res).not.toBeNull() expect(res.status).toBe(200) }) + + it('Should report errors', async () => { + const res = await app.request('http://localhost/sentry/error') + expect(res).not.toBeNull() + expect(res.status).toBe(500) + expect(captureException).toHaveBeenCalled() + }) })