|
1 |
| -import { withSentry, AugmentedNextApiResponse } from '../../src/utils/withSentry'; |
2 |
| -import { NextApiHandler, NextApiRequest } from 'next'; |
3 | 1 | import { Hub, makeMain } from '@sentry/hub';
|
4 | 2 | import { NodeClient } from '@sentry/node';
|
5 | 3 | import { logger } from '@sentry/utils';
|
| 4 | +import { NextApiRequest } from 'next'; |
| 5 | + |
| 6 | +import { AugmentedNextApiResponse, withSentry } from '../../src/utils/withSentry'; |
6 | 7 |
|
7 | 8 | const loggerSpy = jest.spyOn(logger, 'log');
|
| 9 | +let captureExceptionSpy: jest.Mock; |
| 10 | + |
| 11 | +// Prevent captureException from creating and leaving an open handle after test already finished |
| 12 | +jest.mock('@sentry/node', () => { |
| 13 | + const actual = jest.requireActual('@sentry/node'); |
| 14 | + // Mocks are hoisted, thus we need to instentiate a variable here |
| 15 | + captureExceptionSpy = jest.fn(); |
| 16 | + return { |
| 17 | + ...actual, |
| 18 | + captureException: captureExceptionSpy, |
| 19 | + }; |
| 20 | +}); |
8 | 21 |
|
9 |
| -describe('withSentry', async () => { |
10 |
| - it('flushes events before rethrowing error', async done => { |
| 22 | +describe('withSentry', () => { |
| 23 | + it('flushes events before rethrowing error', async () => { |
11 | 24 | const hub = new Hub(
|
12 | 25 | new NodeClient({ dsn: 'https://[email protected]/12312012' }),
|
13 | 26 | );
|
14 | 27 | makeMain(hub);
|
15 |
| - const apiHandler: NextApiHandler = async (_req: any, _res: any) => { |
16 |
| - throw new Error('Charlie ate the flip-flops. :-('); |
17 |
| - }; |
18 | 28 | const req = { url: 'http://dogs.are.great' } as NextApiRequest;
|
19 | 29 | const res = ({ end: async () => undefined } as unknown) as AugmentedNextApiResponse;
|
| 30 | + const error = new Error('Charlie ate the flip-flops. :-('); |
| 31 | + const wrappedHandler = withSentry(async () => { |
| 32 | + throw error; |
| 33 | + }); |
20 | 34 |
|
21 |
| - const wrappedHandler = withSentry(apiHandler); |
22 |
| - expect(async () => await wrappedHandler(req, res)).toThrow(); |
| 35 | + await expect(wrappedHandler(req, res)).rejects.toBe(error); |
23 | 36 | expect(loggerSpy).toHaveBeenCalledWith('Done flushing events');
|
24 |
| - done(); |
| 37 | + expect(captureExceptionSpy).toHaveBeenCalledWith(error); |
25 | 38 | });
|
26 | 39 |
|
27 | 40 | it("doesn't interfere with non-erroring responses", () => {
|
|
0 commit comments