Skip to content

Commit f00f635

Browse files
kamilogoreklobsterkatie
authored andcommitted
Make tests great again
1 parent fc47962 commit f00f635

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

packages/nextjs/src/utils/withSentry.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ export const withSentry = (origHandler: NextApiHandler): WrappedNextApiHandler =
9898
}
9999
console.log('about to call res.end() with the captured error');
100100
(res as AugmentedNextApiResponse).__sentryCapturedError = e;
101-
res.end();
101+
return res.end();
102102
}
103103
});
104104

105-
return await boundHandler();
105+
return boundHandler();
106106
};
107107
};
108108

@@ -137,13 +137,13 @@ function wrapEndMethod(origEnd: ResponseEndMethod): WrappedResponseEndMethod {
137137
logger.log('Done flushing events');
138138
} catch (e) {
139139
logger.log(`Error while flushing events:\n${e}`);
140-
} finally {
141-
if (capturedError) {
142-
console.log('about to rethrow error');
143-
throw capturedError;
144-
}
145-
console.log('about to call origEnd');
146-
return origEnd.call(this, ...args);
147140
}
141+
142+
if (capturedError) {
143+
console.log('about to rethrow error');
144+
throw capturedError;
145+
}
146+
console.log('about to call origEnd');
147+
return origEnd.call(this, ...args);
148148
};
149149
}

packages/nextjs/test/utils/withSentry.test.ts

+23-10
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
1-
import { withSentry, AugmentedNextApiResponse } from '../../src/utils/withSentry';
2-
import { NextApiHandler, NextApiRequest } from 'next';
31
import { Hub, makeMain } from '@sentry/hub';
42
import { NodeClient } from '@sentry/node';
53
import { logger } from '@sentry/utils';
4+
import { NextApiRequest } from 'next';
5+
6+
import { AugmentedNextApiResponse, withSentry } from '../../src/utils/withSentry';
67

78
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+
});
821

9-
describe('withSentry', async () => {
10-
it('flushes events before rethrowing error', async done => {
22+
describe('withSentry', () => {
23+
it('flushes events before rethrowing error', async () => {
1124
const hub = new Hub(
1225
new NodeClient({ dsn: 'https://[email protected]/12312012' }),
1326
);
1427
makeMain(hub);
15-
const apiHandler: NextApiHandler = async (_req: any, _res: any) => {
16-
throw new Error('Charlie ate the flip-flops. :-(');
17-
};
1828
const req = { url: 'http://dogs.are.great' } as NextApiRequest;
1929
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+
});
2034

21-
const wrappedHandler = withSentry(apiHandler);
22-
expect(async () => await wrappedHandler(req, res)).toThrow();
35+
await expect(wrappedHandler(req, res)).rejects.toBe(error);
2336
expect(loggerSpy).toHaveBeenCalledWith('Done flushing events');
24-
done();
37+
expect(captureExceptionSpy).toHaveBeenCalledWith(error);
2538
});
2639

2740
it("doesn't interfere with non-erroring responses", () => {

0 commit comments

Comments
 (0)