Skip to content

Commit bf884b1

Browse files
committed
test(consola): Restructure tests (e.g. changing cases.forEach() to it.each)
1 parent 60969fc commit bf884b1

1 file changed

Lines changed: 68 additions & 168 deletions

File tree

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

Lines changed: 68 additions & 168 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-
};
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, message: `${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+
);
124+
});
224125

225-
sentryReporter.log(logObj);
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, message: `Test ${type}` });
136+
137+
expect(_INTERNAL_captureLog).toHaveBeenCalledWith(
138+
expect.objectContaining({
139+
level: expectedLevel,
140+
message: `Test ${type}`,
141+
attributes: expect.objectContaining({
142+
'consola.type': type,
143+
'sentry.origin': 'auto.log.consola',
144+
}),
145+
}),
146+
);
147+
});
226148

227-
expect(_INTERNAL_captureLog).toHaveBeenCalledWith({
228-
level: 'fatal',
149+
it('uses level number when type is missing', () => {
150+
sentryReporter.log({
151+
level: 0, // Fatal level
229152
message: 'Fatal message',
230-
attributes: {
231-
'sentry.origin': 'auto.log.consola',
232-
'consola.level': 0,
233-
},
234153
});
235-
});
236-
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();
250154

251-
sentryReporter.log({
252-
type,
253-
message: `Test ${type} message`,
254-
});
255-
256-
expect(_INTERNAL_captureLog).toHaveBeenCalledWith({
257-
level: expectedLevel,
258-
message: `Test ${type} message`,
259-
attributes: {
155+
expect(_INTERNAL_captureLog).toHaveBeenCalledWith(
156+
expect.objectContaining({
157+
level: 'fatal',
158+
message: 'Fatal message',
159+
attributes: expect.objectContaining({
160+
'consola.level': 0,
260161
'sentry.origin': 'auto.log.consola',
261-
'consola.type': type,
262-
},
263-
});
264-
});
162+
}),
163+
}),
164+
);
265165
});
266166
});
267167

0 commit comments

Comments
 (0)