Skip to content

Commit 88c6f55

Browse files
committed
add tests
1 parent 684a41f commit 88c6f55

2 files changed

Lines changed: 165 additions & 0 deletions

File tree

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
// Set LAMBDA_TASK_ROOT before any imports so instrumentConsole uses patchWithDefineProperty
2+
process.env.LAMBDA_TASK_ROOT = '/var/task';
3+
4+
import { afterAll, describe, expect, it, vi } from 'vitest';
5+
import { addConsoleInstrumentationHandler } from '../../../src/instrument/console';
6+
import type { WrappedFunction } from '../../../src/types-hoist/wrappedfunction';
7+
import { consoleSandbox, originalConsoleMethods } from '../../../src/utils/debug-logger';
8+
import { markFunctionWrapped } from '../../../src/utils/object';
9+
import { GLOBAL_OBJ } from '../../../src/utils/worldwide';
10+
11+
afterAll(() => {
12+
delete process.env.LAMBDA_TASK_ROOT;
13+
});
14+
15+
describe('addConsoleInstrumentationHandler in Lambda (patchWithDefineProperty)', () => {
16+
it('calls registered handler when console.log is called', () => {
17+
const handler = vi.fn();
18+
addConsoleInstrumentationHandler(handler);
19+
20+
GLOBAL_OBJ.console.log('test');
21+
22+
expect(handler).toHaveBeenCalledWith(expect.objectContaining({ args: ['test'], level: 'log' }));
23+
});
24+
25+
describe('external replacement (e.g. Lambda runtime overwriting console)', () => {
26+
it('keeps firing the handler after console.log is replaced externally', () => {
27+
const handler = vi.fn();
28+
addConsoleInstrumentationHandler(handler);
29+
30+
GLOBAL_OBJ.console.log = vi.fn();
31+
handler.mockClear();
32+
33+
GLOBAL_OBJ.console.log('after replacement');
34+
35+
expect(handler).toHaveBeenCalledWith(expect.objectContaining({ args: ['after replacement'], level: 'log' }));
36+
});
37+
38+
it('calls the external replacement as the underlying method', () => {
39+
addConsoleInstrumentationHandler(vi.fn());
40+
41+
const lambdaLogger = vi.fn();
42+
GLOBAL_OBJ.console.log = lambdaLogger;
43+
44+
GLOBAL_OBJ.console.log('hello');
45+
46+
expect(lambdaLogger).toHaveBeenCalledWith('hello');
47+
});
48+
49+
it('always delegates to the latest replacement', () => {
50+
addConsoleInstrumentationHandler(vi.fn());
51+
52+
const first = vi.fn();
53+
const second = vi.fn();
54+
55+
GLOBAL_OBJ.console.log = first;
56+
GLOBAL_OBJ.console.log = second;
57+
58+
GLOBAL_OBJ.console.log('latest');
59+
60+
expect(first).not.toHaveBeenCalled();
61+
expect(second).toHaveBeenCalledWith('latest');
62+
});
63+
64+
it('updates originalConsoleMethods to point to the replacement', () => {
65+
addConsoleInstrumentationHandler(vi.fn());
66+
67+
const lambdaLogger = vi.fn();
68+
GLOBAL_OBJ.console.log = lambdaLogger;
69+
70+
expect(originalConsoleMethods.log).toBe(lambdaLogger);
71+
});
72+
});
73+
74+
describe('__sentry_original__ detection', () => {
75+
it('accepts a function with __sentry_original__ without re-wrapping', () => {
76+
const handler = vi.fn();
77+
addConsoleInstrumentationHandler(handler);
78+
79+
const otherWrapper = vi.fn();
80+
markFunctionWrapped(otherWrapper as unknown as WrappedFunction, vi.fn() as unknown as WrappedFunction);
81+
82+
GLOBAL_OBJ.console.log = otherWrapper;
83+
84+
expect(GLOBAL_OBJ.console.log).toBe(otherWrapper);
85+
});
86+
87+
it('does not fire our handler when a __sentry_original__ wrapper is installed', () => {
88+
const handler = vi.fn();
89+
addConsoleInstrumentationHandler(handler);
90+
91+
const otherWrapper = vi.fn();
92+
markFunctionWrapped(otherWrapper as unknown as WrappedFunction, vi.fn() as unknown as WrappedFunction);
93+
94+
GLOBAL_OBJ.console.log = otherWrapper;
95+
handler.mockClear();
96+
97+
GLOBAL_OBJ.console.log('via other wrapper');
98+
99+
expect(handler).not.toHaveBeenCalled();
100+
expect(otherWrapper).toHaveBeenCalledWith('via other wrapper');
101+
});
102+
103+
it('re-wraps a plain function without __sentry_original__', () => {
104+
const handler = vi.fn();
105+
addConsoleInstrumentationHandler(handler);
106+
107+
GLOBAL_OBJ.console.log = vi.fn();
108+
handler.mockClear();
109+
110+
GLOBAL_OBJ.console.log('plain');
111+
112+
expect(handler).toHaveBeenCalledWith(expect.objectContaining({ args: ['plain'], level: 'log' }));
113+
});
114+
});
115+
116+
describe('consoleSandbox interaction', () => {
117+
it('does not fire the handler inside consoleSandbox', () => {
118+
const handler = vi.fn();
119+
addConsoleInstrumentationHandler(handler);
120+
handler.mockClear();
121+
122+
consoleSandbox(() => {
123+
GLOBAL_OBJ.console.log('sandbox message');
124+
});
125+
126+
expect(handler).not.toHaveBeenCalled();
127+
});
128+
129+
it('resumes firing the handler after consoleSandbox returns', () => {
130+
const handler = vi.fn();
131+
addConsoleInstrumentationHandler(handler);
132+
133+
consoleSandbox(() => {
134+
GLOBAL_OBJ.console.log('inside sandbox');
135+
});
136+
handler.mockClear();
137+
138+
GLOBAL_OBJ.console.log('after sandbox');
139+
140+
expect(handler).toHaveBeenCalledWith(expect.objectContaining({ args: ['after sandbox'], level: 'log' }));
141+
});
142+
});
143+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { describe, expect, it, vi } from 'vitest';
2+
import { addConsoleInstrumentationHandler } from '../../../src/instrument/console';
3+
import { GLOBAL_OBJ } from '../../../src/utils/worldwide';
4+
5+
describe('addConsoleInstrumentationHandler', () => {
6+
it.each(['log', 'warn', 'error', 'debug', 'info'] as const)(
7+
'calls registered handler when console.%s is called',
8+
level => {
9+
const handler = vi.fn();
10+
addConsoleInstrumentationHandler(handler);
11+
12+
GLOBAL_OBJ.console[level]('test message');
13+
14+
expect(handler).toHaveBeenCalledWith(expect.objectContaining({ args: ['test message'], level }));
15+
},
16+
);
17+
18+
it('calls through to the underlying console method without throwing', () => {
19+
addConsoleInstrumentationHandler(vi.fn());
20+
expect(() => GLOBAL_OBJ.console.log('hello')).not.toThrow();
21+
});
22+
});

0 commit comments

Comments
 (0)