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

Commit cde6677

Browse files
author
Michal Vlasák
committed
✨ Add support for 3-param calls for Sentry extend
1 parent e38e4d6 commit cde6677

2 files changed

Lines changed: 46 additions & 4 deletions

File tree

src/sentry.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ export const extendSentry = (logger: Cosmas, options: { sentry: string | true; s
5151
};
5252

5353
logger.realHooks.logMethod = function (inputArgs, method) {
54-
// 2nd argument is Sentry options
55-
const sentryCallback: SentryCallback | undefined = inputArgs[1];
56-
// TODO: automatic type for 2nd log method arg
54+
// TODO: automatic types for logFn calls
55+
const sentryCallback: SentryCallback | undefined =
56+
typeof inputArgs[0] === 'object' ? inputArgs[2] : inputArgs[1];
5757

5858
if (!sentryCallback) {
5959
return method.apply(this, inputArgs);

src/tests/sentry-mocked.test.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,49 @@ describe('sentry mocked', () => {
131131
});
132132
});
133133

134-
test('can pass sentry tags', async () => {
134+
test('can pass sentry tags, context and extras', async () => {
135+
const dateNow = Date.now;
136+
Date.now = jest.fn(() => 1520343036000);
137+
await new Promise((resolve, reject) => {
138+
const logger = loggerFactory();
139+
extendSentry(logger, { sentry: 'DSN' });
140+
captureMessage.mockImplementation(createCapture(resolve));
141+
logger.fatal({ name: 'John Doe' }, 'sentryData', (scope: Scope) => {
142+
scope.setContext('dummyContext', { foo: 'bar' });
143+
scope.setTags({ first: 'firstTag' });
144+
scope.setExtras({ extra: 'extraValue' });
145+
});
146+
});
147+
expect(captureMessage).toHaveBeenCalledTimes(1);
148+
expect(captureException).not.toHaveBeenCalled();
149+
expect(captureMessage.mock.calls[0]).toMatchInlineSnapshot(`
150+
Array [
151+
"sentryData",
152+
]
153+
`);
154+
expect(omit(captureMessage.mock.results[0].value, 'cosmas.pkgVersion')).toMatchInlineSnapshot(`
155+
Object {
156+
"data": "sentryData",
157+
"scope": Object {
158+
"context": Object {
159+
"dummyContext": Object {
160+
"foo": "bar",
161+
},
162+
},
163+
"extras": Object {
164+
"extra": "extraValue",
165+
},
166+
"level": "critical",
167+
"tags": Object {
168+
"first": "firstTag",
169+
},
170+
},
171+
}
172+
`);
173+
Date.now = dateNow;
174+
});
175+
176+
test('can pass sentry tags, context and extras with 3 params', async () => {
135177
const dateNow = Date.now;
136178
Date.now = jest.fn(() => 1520343036000);
137179
await new Promise((resolve, reject) => {

0 commit comments

Comments
 (0)