Skip to content

Commit 17b7532

Browse files
authored
test(consola): Restructure tests (e.g. changing cases.forEach() to it.each) (#19517)
Some small refactoring to make test cases better readable. Part of this PR (to make things easier reviewable): #18602 Closes #19518 (added automatically)
1 parent 89b437f commit 17b7532

File tree

2 files changed

+81
-174
lines changed

2 files changed

+81
-174
lines changed

packages/core/src/integrations/consola.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,20 @@ export interface ConsolaLogObject {
123123
/**
124124
* The raw arguments passed to the log method.
125125
*
126-
* When `message` is not provided, these args are typically formatted into the final message.
126+
* These args are typically formatted into the final `message`. In Consola reporters, `message` is not provided.
127127
*
128128
* @example
129129
* ```ts
130130
* consola.info('Hello', 'world', { user: 'john' });
131131
* // args = ['Hello', 'world', { user: 'john' }]
132132
* ```
133+
*
134+
* @example
135+
* ```ts
136+
* // `message` is a reserved property in Consola
137+
* consola.log({ message: 'Hello' });
138+
* // args = ['Hello']
139+
* ```
133140
*/
134141
args?: unknown[];
135142

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

Lines changed: 73 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ vi.mock('../../../src/logs/internal', () => ({
1111
_INTERNAL_flushLogsBuffer: vi.fn(),
1212
}));
1313

14-
vi.mock('../../../src/logs/utils', async actual => ({
15-
formatConsoleArgs: vi.fn(((await actual()) as any).formatConsoleArgs),
16-
}));
14+
vi.mock('../../../src/logs/utils', async importOriginal => {
15+
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
16+
const actual: typeof import('../../../src/logs/utils') = await importOriginal();
17+
return {
18+
...actual,
19+
formatConsoleArgs: vi.fn(actual.formatConsoleArgs),
20+
};
21+
});
1722

1823
vi.mock('../../../src/currentScopes', () => ({
1924
getClient: vi.fn(),
@@ -22,6 +27,7 @@ vi.mock('../../../src/currentScopes', () => ({
2227

2328
describe('createConsolaReporter', () => {
2429
let mockClient: TestClient;
30+
let sentryReporter: ReturnType<typeof createConsolaReporter>;
2531

2632
beforeEach(() => {
2733
vi.clearAllMocks();
@@ -34,12 +40,11 @@ describe('createConsolaReporter', () => {
3440
normalizeMaxBreadth: 1000,
3541
});
3642

37-
const mockScope = {
38-
getClient: vi.fn().mockReturnValue(mockClient),
39-
};
40-
4143
vi.mocked(getClient).mockReturnValue(mockClient);
42-
vi.mocked(getCurrentScope).mockReturnValue(mockScope as any);
44+
vi.mocked(getCurrentScope).mockReturnValue({
45+
getClient: vi.fn().mockReturnValue(mockClient),
46+
} as any);
47+
sentryReporter = createConsolaReporter();
4348
});
4449

4550
afterEach(() => {
@@ -56,126 +61,7 @@ describe('createConsolaReporter', () => {
5661
});
5762
});
5863

59-
describe('log capturing', () => {
60-
let sentryReporter: any;
61-
62-
beforeEach(() => {
63-
sentryReporter = createConsolaReporter();
64-
});
65-
66-
it('should capture error logs', () => {
67-
const logObj = {
68-
type: 'error',
69-
level: 0,
70-
message: 'This is an error',
71-
tag: 'test',
72-
date: new Date('2023-01-01T00:00:00.000Z'),
73-
};
74-
75-
sentryReporter.log(logObj);
76-
77-
expect(_INTERNAL_captureLog).toHaveBeenCalledWith({
78-
level: 'error',
79-
message: 'This is an error',
80-
attributes: {
81-
'sentry.origin': 'auto.log.consola',
82-
'consola.tag': 'test',
83-
'consola.type': 'error',
84-
'consola.level': 0,
85-
},
86-
});
87-
});
88-
89-
it('should capture warn logs', () => {
90-
const logObj = {
91-
type: 'warn',
92-
message: 'This is a warning',
93-
};
94-
95-
sentryReporter.log(logObj);
96-
97-
expect(_INTERNAL_captureLog).toHaveBeenCalledWith({
98-
level: 'warn',
99-
message: 'This is a warning',
100-
attributes: {
101-
'sentry.origin': 'auto.log.consola',
102-
'consola.type': 'warn',
103-
},
104-
});
105-
});
106-
107-
it('should capture info logs', () => {
108-
const logObj = {
109-
type: 'info',
110-
message: 'This is info',
111-
};
112-
113-
sentryReporter.log(logObj);
114-
115-
expect(_INTERNAL_captureLog).toHaveBeenCalledWith({
116-
level: 'info',
117-
message: 'This is info',
118-
attributes: {
119-
'sentry.origin': 'auto.log.consola',
120-
'consola.type': 'info',
121-
},
122-
});
123-
});
124-
125-
it('should capture debug logs', () => {
126-
const logObj = {
127-
type: 'debug',
128-
message: 'Debug message',
129-
};
130-
131-
sentryReporter.log(logObj);
132-
133-
expect(_INTERNAL_captureLog).toHaveBeenCalledWith({
134-
level: 'debug',
135-
message: 'Debug message',
136-
attributes: {
137-
'sentry.origin': 'auto.log.consola',
138-
'consola.type': 'debug',
139-
},
140-
});
141-
});
142-
143-
it('should capture trace logs', () => {
144-
const logObj = {
145-
type: 'trace',
146-
message: 'Trace message',
147-
};
148-
149-
sentryReporter.log(logObj);
150-
151-
expect(_INTERNAL_captureLog).toHaveBeenCalledWith({
152-
level: 'trace',
153-
message: 'Trace message',
154-
attributes: {
155-
'sentry.origin': 'auto.log.consola',
156-
'consola.type': 'trace',
157-
},
158-
});
159-
});
160-
161-
it('should capture fatal logs', () => {
162-
const logObj = {
163-
type: 'fatal',
164-
message: 'Fatal error',
165-
};
166-
167-
sentryReporter.log(logObj);
168-
169-
expect(_INTERNAL_captureLog).toHaveBeenCalledWith({
170-
level: 'fatal',
171-
message: 'Fatal error',
172-
attributes: {
173-
'sentry.origin': 'auto.log.consola',
174-
'consola.type': 'fatal',
175-
},
176-
});
177-
});
178-
64+
describe('message and args handling', () => {
17965
it('should format message from args when message is not provided', () => {
18066
const logObj = {
18167
type: 'info',
@@ -215,53 +101,67 @@ describe('createConsolaReporter', () => {
215101
},
216102
});
217103
});
104+
});
218105

219-
it('should map consola levels to sentry levels when type is not provided', () => {
220-
const logObj = {
221-
level: 0, // Fatal level
222-
message: 'Fatal message',
223-
};
224-
225-
sentryReporter.log(logObj);
226-
227-
expect(_INTERNAL_captureLog).toHaveBeenCalledWith({
228-
level: 'fatal',
229-
message: 'Fatal message',
230-
attributes: {
231-
'sentry.origin': 'auto.log.consola',
232-
'consola.level': 0,
233-
},
234-
});
106+
describe('level mapping', () => {
107+
it.each([
108+
['error', 'error'],
109+
['warn', 'warn'],
110+
['info', 'info'],
111+
['debug', 'debug'],
112+
['trace', 'trace'],
113+
['fatal', 'fatal'],
114+
] as const)('maps type "%s" to Sentry level "%s"', (type, expectedLevel) => {
115+
sentryReporter.log({ type, args: [`${type} message`] });
116+
117+
expect(_INTERNAL_captureLog).toHaveBeenCalledWith(
118+
expect.objectContaining({
119+
level: expectedLevel,
120+
message: `${type} message`,
121+
attributes: expect.objectContaining({ 'consola.type': type, 'sentry.origin': 'auto.log.consola' }),
122+
}),
123+
);
235124
});
236125

237-
it('should map various consola types correctly', () => {
238-
const testCases = [
239-
{ type: 'success', expectedLevel: 'info' },
240-
{ type: 'fail', expectedLevel: 'error' },
241-
{ type: 'ready', expectedLevel: 'info' },
242-
{ type: 'start', expectedLevel: 'info' },
243-
{ type: 'verbose', expectedLevel: 'debug' },
244-
{ type: 'log', expectedLevel: 'info' },
245-
{ type: 'silent', expectedLevel: 'trace' },
246-
];
247-
248-
testCases.forEach(({ type, expectedLevel }) => {
249-
vi.clearAllMocks();
250-
251-
sentryReporter.log({
252-
type,
253-
message: `Test ${type} message`,
254-
});
255-
256-
expect(_INTERNAL_captureLog).toHaveBeenCalledWith({
126+
it.each([
127+
['success', 'info'],
128+
['fail', 'error'],
129+
['ready', 'info'],
130+
['start', 'info'],
131+
['verbose', 'debug'],
132+
['log', 'info'],
133+
['silent', 'trace'],
134+
] as const)('maps consola type "%s" to Sentry level "%s"', (type, expectedLevel) => {
135+
sentryReporter.log({ type, args: [`Test ${type}`] });
136+
137+
expect(_INTERNAL_captureLog).toHaveBeenCalledWith(
138+
expect.objectContaining({
257139
level: expectedLevel,
258-
message: `Test ${type} message`,
259-
attributes: {
260-
'sentry.origin': 'auto.log.consola',
140+
message: `Test ${type}`,
141+
attributes: expect.objectContaining({
261142
'consola.type': type,
262-
},
263-
});
143+
'sentry.origin': 'auto.log.consola',
144+
}),
145+
}),
146+
);
147+
});
148+
149+
it('uses level number when type is missing', () => {
150+
sentryReporter.log({
151+
level: 0, // Fatal level
152+
args: ['Fatal message'],
264153
});
154+
155+
expect(_INTERNAL_captureLog).toHaveBeenCalledWith(
156+
expect.objectContaining({
157+
level: 'fatal',
158+
message: 'Fatal message',
159+
attributes: expect.objectContaining({
160+
'consola.level': 0,
161+
'sentry.origin': 'auto.log.consola',
162+
}),
163+
}),
164+
);
265165
});
266166
});
267167

@@ -274,21 +174,21 @@ describe('createConsolaReporter', () => {
274174
// Should capture error
275175
filteredReporter.log({
276176
type: 'error',
277-
message: 'Error message',
177+
args: ['Error message'],
278178
});
279179
expect(_INTERNAL_captureLog).toHaveBeenCalledTimes(1);
280180

281181
// Should capture warn
282182
filteredReporter.log({
283183
type: 'warn',
284-
message: 'Warn message',
184+
args: ['Warn message'],
285185
});
286186
expect(_INTERNAL_captureLog).toHaveBeenCalledTimes(2);
287187

288188
// Should not capture info
289189
filteredReporter.log({
290190
type: 'info',
291-
message: 'Info message',
191+
args: ['Info message'],
292192
});
293193
expect(_INTERNAL_captureLog).toHaveBeenCalledTimes(2);
294194
});
@@ -300,7 +200,7 @@ describe('createConsolaReporter', () => {
300200
['trace', 'debug', 'info', 'warn', 'error', 'fatal'].forEach(type => {
301201
defaultReporter.log({
302202
type,
303-
message: `${type} message`,
203+
args: [`${type} message`],
304204
});
305205
});
306206

0 commit comments

Comments
 (0)