-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathexports.test.ts
More file actions
222 lines (204 loc) · 6.85 KB
/
exports.test.ts
File metadata and controls
222 lines (204 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import * as sentryCore from '@sentry/core';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import * as nodeLogger from '../../src/logs/exports';
import { Scope } from '@sentry/core';
// Mock the core functions
vi.mock('@sentry/core', async () => {
const actual = await vi.importActual('@sentry/core');
return {
...actual,
_INTERNAL_captureLog: vi.fn(),
};
});
describe('Node Logger', () => {
// Use the mocked function
const mockCaptureLog = vi.mocked(sentryCore._INTERNAL_captureLog);
beforeEach(() => {
// Reset mocks
mockCaptureLog.mockClear();
});
afterEach(() => {
vi.resetAllMocks();
});
describe('Basic logging methods', () => {
it('should export all log methods', () => {
expect(nodeLogger.trace).toBeTypeOf('function');
expect(nodeLogger.debug).toBeTypeOf('function');
expect(nodeLogger.info).toBeTypeOf('function');
expect(nodeLogger.warn).toBeTypeOf('function');
expect(nodeLogger.error).toBeTypeOf('function');
expect(nodeLogger.fatal).toBeTypeOf('function');
});
it('should call _INTERNAL_captureLog with trace level', () => {
nodeLogger.trace('Test trace message', { key: 'value' });
expect(mockCaptureLog).toHaveBeenCalledWith(
{
level: 'trace',
message: 'Test trace message',
attributes: { key: 'value' },
},
undefined,
);
});
it('should call _INTERNAL_captureLog with debug level', () => {
nodeLogger.debug('Test debug message', { key: 'value' });
expect(mockCaptureLog).toHaveBeenCalledWith(
{
level: 'debug',
message: 'Test debug message',
attributes: { key: 'value' },
},
undefined,
);
});
it('should call _INTERNAL_captureLog with info level', () => {
nodeLogger.info('Test info message', { key: 'value' });
expect(mockCaptureLog).toHaveBeenCalledWith(
{
level: 'info',
message: 'Test info message',
attributes: { key: 'value' },
},
undefined,
);
});
it('should call _INTERNAL_captureLog with warn level', () => {
nodeLogger.warn('Test warn message', { key: 'value' });
expect(mockCaptureLog).toHaveBeenCalledWith(
{
level: 'warn',
message: 'Test warn message',
attributes: { key: 'value' },
},
undefined,
);
});
it('should call _INTERNAL_captureLog with error level', () => {
nodeLogger.error('Test error message', { key: 'value' });
expect(mockCaptureLog).toHaveBeenCalledWith(
{
level: 'error',
message: 'Test error message',
attributes: { key: 'value' },
},
undefined,
);
});
it('should call _INTERNAL_captureLog with fatal level', () => {
nodeLogger.fatal('Test fatal message', { key: 'value' });
expect(mockCaptureLog).toHaveBeenCalledWith(
{
level: 'fatal',
message: 'Test fatal message',
attributes: { key: 'value' },
},
undefined,
);
});
});
describe('Template string logging', () => {
it('should handle template strings with parameters', () => {
nodeLogger.info('Hello %s, your balance is %d', ['John', 100], { userId: 123 });
expect(mockCaptureLog).toHaveBeenCalledWith(
{
level: 'info',
message: 'Hello John, your balance is 100',
attributes: {
userId: 123,
'sentry.message.template': 'Hello %s, your balance is %d',
'sentry.message.parameter.0': 'John',
'sentry.message.parameter.1': 100,
},
},
undefined,
);
});
it('should handle template strings without additional attributes', () => {
nodeLogger.debug('User %s logged in from %s', ['Alice', 'mobile']);
expect(mockCaptureLog).toHaveBeenCalledWith(
{
level: 'debug',
message: 'User Alice logged in from mobile',
attributes: {
'sentry.message.template': 'User %s logged in from %s',
'sentry.message.parameter.0': 'Alice',
'sentry.message.parameter.1': 'mobile',
},
},
undefined,
);
});
it('should handle parameterized strings with parameters', () => {
nodeLogger.info(nodeLogger.fmt`Hello ${'John'}, your balance is ${100}`, { userId: 123 });
expect(mockCaptureLog).toHaveBeenCalledWith(
{
level: 'info',
message: expect.objectContaining({
__sentry_template_string__: 'Hello %s, your balance is %s',
__sentry_template_values__: ['John', 100],
}),
attributes: {
userId: 123,
},
},
undefined,
);
});
it('should handle parameterized strings without additional attributes', () => {
nodeLogger.debug(nodeLogger.fmt`User ${'Alice'} logged in from ${'mobile'}`);
expect(mockCaptureLog).toHaveBeenCalledWith(
{
level: 'debug',
message: expect.objectContaining({
__sentry_template_string__: 'User %s logged in from %s',
__sentry_template_values__: ['Alice', 'mobile'],
}),
},
undefined,
);
});
});
describe('scustom cope', () => {
it('calls _INTERNAL_captureLog with custom scope for basic log message', () => {
const customScope = new Scope();
nodeLogger.debug('User logged in', undefined, { scope: customScope });
expect(mockCaptureLog).toHaveBeenCalledWith(
{
level: 'debug',
message: 'User logged in',
},
customScope,
);
});
it('calls _INTERNAL_captureLog with custom scope for parametrized log message', () => {
const customScope = new Scope();
nodeLogger.debug('User %s logged in from %s', ['Alice', 'mobile'], undefined, { scope: customScope });
expect(mockCaptureLog).toHaveBeenCalledWith(
{
level: 'debug',
message: 'User Alice logged in from mobile',
attributes: {
'sentry.message.template': 'User %s logged in from %s',
'sentry.message.parameter.0': 'Alice',
'sentry.message.parameter.1': 'mobile',
},
},
customScope,
);
});
it('calls _INTERNAL_captureLog with custom scope for fmt log message', () => {
const customScope = new Scope();
nodeLogger.debug(nodeLogger.fmt`User ${'Alice'} logged in from ${'mobile'}`, undefined, { scope: customScope });
expect(mockCaptureLog).toHaveBeenCalledWith(
{
level: 'debug',
message: expect.objectContaining({
__sentry_template_string__: 'User %s logged in from %s',
__sentry_template_values__: ['Alice', 'mobile'],
}),
},
customScope,
);
});
});
});