Skip to content

Commit 6db152d

Browse files
committed
fix tests
1 parent ab85873 commit 6db152d

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;
@@ -153,6 +154,10 @@ export interface ConsolaLogObject {
153154
*
154155
* When provided, this is the final formatted message. When not provided,
155156
* the message should be constructed from the `args` array.
157+
*
158+
* Note: In reporters, `message` is typically undefined. It is primarily for
159+
* `consola.[type]({ message: 'xxx' })` usage and is normalized into `args` before
160+
* reporters receive the log object. See: https://github.com/unjs/consola/issues/406#issuecomment-3684792551
156161
*/
157162
message?: string;
158163
}
@@ -199,8 +204,6 @@ export function createConsolaReporter(options: ConsolaReporterOptions = {}): Con
199204
// eslint-disable-next-line @typescript-eslint/no-unused-vars
200205
const { type, level, message: consolaMessage, args, tag, date: _date, ...rest } = logObj;
201206

202-
const hasExtraLogObjKeys = Object.keys(rest).length > 0;
203-
204207
// Get client - use provided client or current client
205208
const client = providedClient || getClient();
206209
if (!client) {
@@ -229,12 +232,11 @@ export function createConsolaReporter(options: ConsolaReporterOptions = {}): Con
229232

230233
const attributes: Record<string, unknown> = {};
231234

232-
// Build attributes: `rest` properties from logObj get a "consola" prefix; base attributes added below may override
235+
// Build attributes
233236
for (const [key, value] of Object.entries(rest)) {
234-
attributes[`consola.${key}`] = normalize(value, normalizeDepth, normalizeMaxBreadth);
237+
attributes[key] = normalize(value, normalizeDepth, normalizeMaxBreadth);
235238
}
236239

237-
// Build attributes
238240
attributes['sentry.origin'] = 'auto.log.consola';
239241

240242
if (tag) {

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ describe('createConsolaReporter', () => {
208208
});
209209
});
210210

211-
describe('direct reporter call (extra log keys)', () => {
212-
it('consola-merged: args=[message] with extra keys on logObj', () => {
211+
describe('message and args handling', () => {
212+
it('consola-merged: args=[message] with extra keys on log object', () => {
213213
sentryReporter.log({
214214
type: 'log',
215215
level: 2,
@@ -228,16 +228,16 @@ describe('createConsolaReporter', () => {
228228
expect(call.attributes).toMatchObject({
229229
'consola.type': 'log',
230230
'consola.level': 2,
231-
'consola.userId': 123,
232-
'consola.smallObj': { firstLevel: { secondLevel: { thirdLevel: '[Object]' } } }, // Object is normalized
233-
'consola.action': 'login',
234-
'consola.time': '2026-02-24T10:24:04.477Z',
231+
userId: 123,
232+
smallObj: { firstLevel: { secondLevel: { thirdLevel: '[Object]' } } }, // Object is normalized
233+
action: 'login',
234+
time: '2026-02-24T10:24:04.477Z',
235235
'sentry.origin': 'auto.log.consola',
236236
});
237237
expect(call.attributes?.['sentry.message.parameter.0']).toBeUndefined();
238238
});
239239

240-
it('direct reporter.log({ type, message, userId, sessionId }) captures custom keys with consola. prefix', () => {
240+
it('capturing custom keys mimicking `log({ message: "", ... })` or direct reporter.log({ type, message, userId, sessionId })', () => {
241241
sentryReporter.log({
242242
type: 'info',
243243
message: 'User action',
@@ -249,8 +249,8 @@ describe('createConsolaReporter', () => {
249249
expect(call.message).toBe('User action');
250250
expect(call.attributes).toMatchObject({
251251
'consola.type': 'info',
252-
'consola.userId': 123,
253-
'consola.sessionId': 'abc-123',
252+
userId: 123,
253+
sessionId: 'abc-123',
254254
});
255255
});
256256
});

0 commit comments

Comments
 (0)