Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
✅ Add capture exception tests
Browse files Browse the repository at this point in the history
Related: #28
  • Loading branch information
smolijar committed Nov 25, 2019
1 parent d3da837 commit 8f440fc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
1 change: 0 additions & 1 deletion src/sentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class SentryTransformStream extends Transform {
60: Severity.Critical,
};
const obj = JSON.parse(chunk);
captureException(obj);
withScope(scope => {
scope.setLevel(PINO_TO_SENTRY[obj.level]);
scope.setContext('data', obj);
Expand Down
50 changes: 36 additions & 14 deletions src/tests/sentry-mocked.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('sentry mocked', () => {
jest.mock('@sentry/node', () => {
return {
captureException,
captureMessage,
withScope,
Severity: {
Debug: 'debug',
Expand All @@ -36,35 +37,33 @@ describe('sentry mocked', () => {
});
loggerFactory = require('..').default;
});
beforeEach(() => {
captureException.mockReset();
captureMessage.mockReset();
});
test('can create logger with options', () => {
expect(() => loggerFactory()).not.toThrowError();
expect(() => loggerFactory({ sentryDsn: 'DSN' })).not.toThrowError();
});

test('sentry is called with correct scope', async () => {
test('sentry captureMessage is called with correct scope', async () => {
await new Promise((resolve, reject) => {
const logger = loggerFactory({
sentryDsn: 'DSN',
});
captureException.mockImplementation(createCapture(resolve));
captureMessage.mockImplementation(createCapture(resolve));
logger.info('Foo');
});
expect(captureException.mock.calls[0]).toMatchInlineSnapshot(`
expect(captureMessage).toHaveBeenCalledTimes(1);
expect(captureException).not.toHaveBeenCalled();
expect(captureMessage.mock.calls[0]).toMatchInlineSnapshot(`
Array [
Object {
"level": 30,
"message": "Foo",
"v": 1,
},
"Foo",
]
`);
expect(captureException.mock.results[0].value).toMatchInlineSnapshot(`
expect(captureMessage.mock.results[0].value).toMatchInlineSnapshot(`
Object {
"data": Object {
"level": 30,
"message": "Foo",
"v": 1,
},
"data": "Foo",
"scope": Object {
"context": Object {
"data": Object {
Expand All @@ -78,4 +77,27 @@ describe('sentry mocked', () => {
}
`);
});

test('sentry captureException with stack and correct levels', async () => {
await new Promise((resolve, reject) => {
const logger = loggerFactory({
sentryDsn: 'DSN',
});
captureException.mockReset();
captureException.mockImplementation(createCapture(resolve));
logger.error(new Error());
});
expect(captureException).toHaveBeenCalledTimes(1);
expect(captureMessage).not.toHaveBeenCalled();
expect(captureException.mock.results[0].value).toMatchObject({
data: {
level: 50,
message: expect.any(String),
stack: expect.any(String),
},
scope: {
level: 'error',
},
});
});
});

0 comments on commit 8f440fc

Please sign in to comment.