Skip to content

Commit 676b162

Browse files
committed
fix tests
1 parent fbfce7a commit 676b162

2 files changed

Lines changed: 19 additions & 17 deletions

File tree

packages/core/src/integrations/consola.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ export interface ConsolaReporter {
6262
*/
6363
export interface ConsolaLogObject {
6464
/**
65-
* Allows additional custom properties to be set on the log object (e.g. when reporter is called directly)
66-
* These properties will be captured as log attributes with a 'consola.' prefix.
65+
* Allows additional custom properties to be set on the log object. These properties will be captured as log attributes.
66+
*
67+
* Additional properties are set when passing a single object with a `message` (`consola.[type]({ message: '', ... })`) or if the reporter is called directly
6768
*
6869
* @example
6970
* ```ts
@@ -74,7 +75,7 @@ export interface ConsolaLogObject {
7475
* userId: 123,
7576
* sessionId: 'abc-123'
7677
* });
77-
* // Will create attributes: consola.userId and consola.sessionId
78+
* // Will create attributes: `userId` and `sessionId`
7879
* ```
7980
*/
8081
[key: string]: unknown;
@@ -146,6 +147,10 @@ export interface ConsolaLogObject {
146147
*
147148
* When provided, this is the final formatted message. When not provided,
148149
* the message should be constructed from the `args` array.
150+
*
151+
* Note: In reporters, `message` is typically undefined. It is primarily for
152+
* `consola.[type]({ message: 'xxx' })` usage and is normalized into `args` before
153+
* reporters receive the log object. See: https://github.com/unjs/consola/issues/406#issuecomment-3684792551
149154
*/
150155
message?: string;
151156
}
@@ -192,8 +197,6 @@ export function createConsolaReporter(options: ConsolaReporterOptions = {}): Con
192197
// eslint-disable-next-line @typescript-eslint/no-unused-vars
193198
const { type, level, message: consolaMessage, args, tag, date: _date, ...rest } = logObj;
194199

195-
const hasExtraLogObjKeys = Object.keys(rest).length > 0;
196-
197200
// Get client - use provided client or current client
198201
const client = providedClient || getClient();
199202
if (!client) {
@@ -222,12 +225,11 @@ export function createConsolaReporter(options: ConsolaReporterOptions = {}): Con
222225

223226
const attributes: Record<string, unknown> = {};
224227

225-
// Build attributes: `rest` properties from logObj get a "consola" prefix; base attributes added below may override
228+
// Build attributes
226229
for (const [key, value] of Object.entries(rest)) {
227-
attributes[`consola.${key}`] = normalize(value, normalizeDepth, normalizeMaxBreadth);
230+
attributes[key] = normalize(value, normalizeDepth, normalizeMaxBreadth);
228231
}
229232

230-
// Build attributes
231233
attributes['sentry.origin'] = 'auto.log.consola';
232234

233235
if (tag) {

packages/core/test/lib/integrations/consola.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,8 @@ describe('createConsolaReporter', () => {
310310
});
311311
});
312312

313-
describe('direct reporter call (extra log keys)', () => {
314-
it('consola-merged: args=[message] with extra keys on logObj', () => {
313+
describe('message and args handling', () => {
314+
it('consola-merged: args=[message] with extra keys on log object', () => {
315315
sentryReporter.log({
316316
type: 'log',
317317
level: 2,
@@ -330,16 +330,16 @@ describe('createConsolaReporter', () => {
330330
expect(call.attributes).toMatchObject({
331331
'consola.type': 'log',
332332
'consola.level': 2,
333-
'consola.userId': 123,
334-
'consola.smallObj': { firstLevel: { secondLevel: { thirdLevel: '[Object]' } } }, // Object is normalized
335-
'consola.action': 'login',
336-
'consola.time': '2026-02-24T10:24:04.477Z',
333+
userId: 123,
334+
smallObj: { firstLevel: { secondLevel: { thirdLevel: '[Object]' } } }, // Object is normalized
335+
action: 'login',
336+
time: '2026-02-24T10:24:04.477Z',
337337
'sentry.origin': 'auto.log.consola',
338338
});
339339
expect(call.attributes?.['sentry.message.parameter.0']).toBeUndefined();
340340
});
341341

342-
it('direct reporter.log({ type, message, userId, sessionId }) captures custom keys with consola. prefix', () => {
342+
it('capturing custom keys mimicking `log({ message: "", ... })` or direct reporter.log({ type, message, userId, sessionId })', () => {
343343
sentryReporter.log({
344344
type: 'info',
345345
message: 'User action',
@@ -351,8 +351,8 @@ describe('createConsolaReporter', () => {
351351
expect(call.message).toBe('User action');
352352
expect(call.attributes).toMatchObject({
353353
'consola.type': 'info',
354-
'consola.userId': 123,
355-
'consola.sessionId': 'abc-123',
354+
userId: 123,
355+
sessionId: 'abc-123',
356356
});
357357
});
358358
});

0 commit comments

Comments
 (0)