Skip to content

Commit 18fff70

Browse files
kamilogoreklobsterkatie
authored andcommitted
Make tests great again
1 parent 7060d0b commit 18fff70

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

packages/nextjs/src/utils/withSentry.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ export const withSentry = (origHandler: NextApiHandler): WrappedNextApiHandler =
9797
}
9898
console.log('about to call res.end() with the captured error');
9999
(res as AugmentedNextApiResponse).__sentryCapturedError = e;
100-
res.end();
100+
return res.end();
101101
}
102102
});
103103

104-
return await boundHandler();
104+
return boundHandler();
105105
};
106106
};
107107

@@ -136,13 +136,13 @@ function wrapEndMethod(origEnd: ResponseEndMethod): WrappedResponseEndMethod {
136136
logger.log('Done flushing events');
137137
} catch (e) {
138138
logger.log(`Error while flushing events:\n${e}`);
139-
} finally {
140-
if (capturedError) {
141-
console.log('about to rethrow error');
142-
throw capturedError;
143-
}
144-
console.log('about to call origEnd');
145-
return origEnd.call(this, ...args);
146139
}
140+
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);
147147
};
148148
}

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

Lines changed: 23 additions & 10 deletions
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://dogsarebadatkeepingsecrets@squirrelchasers.ingest.sentry.io/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)