-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathhelpers.test.ts
177 lines (140 loc) · 5.15 KB
/
helpers.test.ts
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
import type { WrappedFunction } from '@sentry/types';
import { spy } from 'sinon';
import { wrap } from '../../../src/helpers';
describe('internal wrap()', () => {
it('should wrap only functions', () => {
const fn = (): number => 1337;
const obj = { pickle: 'Rick' };
const arr = ['Morty'];
const str = 'Rick';
const num = 42;
expect(wrap(fn)).not.toBe(fn);
// @ts-ignore Issue with `WrappedFunction` type from wrap fn
expect(wrap(obj)).toBe(obj);
// @ts-ignore Issue with `WrappedFunction` type from wrap fn
expect(wrap(arr)).toBe(arr);
// @ts-ignore Issue with `WrappedFunction` type from wrap fn
expect(wrap(str)).toBe(str);
// @ts-ignore Issue with `WrappedFunction` type from wrap fn
expect(wrap(num)).toBe(num);
});
it('should preserve correct function name when accessed', () => {
const namedFunction = (): number => 1337;
expect(wrap(namedFunction)).not.toBe(namedFunction);
expect(namedFunction.name).toBe('namedFunction');
expect(wrap(namedFunction).name).toBe('namedFunction');
});
it('bail out with the original if accessing custom props go bad', () => {
const fn = (() => 1337) as WrappedFunction;
Object.defineProperty(fn, '__sentry_wrapped__', {
get(): void {
throw new Error('boom');
},
});
expect(wrap(fn)).toBe(fn);
});
it('returns wrapped function if original was already wrapped', () => {
const fn = (() => 1337) as WrappedFunction;
const wrapped = wrap(fn);
expect(wrap(fn)).toBe(wrapped);
});
it('returns same wrapped function if trying to wrap it again', () => {
const fn = (() => 1337) as WrappedFunction;
const wrapped = wrap(fn);
expect(wrap(wrapped)).toBe(wrapped);
});
it('calls "before" function when invoking wrapped function', () => {
const fn = (() => 1337) as WrappedFunction;
const before = spy();
const wrapped = wrap(fn, {}, before);
wrapped();
expect(before.called).toBe(true);
});
it('attaches metadata to original and wrapped functions', () => {
const fn = (() => 1337) as WrappedFunction;
const wrapped = wrap(fn);
expect(fn).toHaveProperty('__sentry_wrapped__');
expect(fn.__sentry_wrapped__).toBe(wrapped);
expect(wrapped).toHaveProperty('__sentry_original__');
expect(wrapped.__sentry_original__).toBe(fn);
});
it('copies over original functions properties', () => {
const fn = (() => 1337) as WrappedFunction;
fn.some = 1337;
fn.property = 'Rick';
const wrapped = wrap(fn);
expect(wrapped).toHaveProperty('some');
expect(wrapped.some).toBe(1337);
expect(wrapped).toHaveProperty('property');
expect(wrapped.property).toBe('Rick');
});
it('doesnt break when accessing original functions properties blows up', () => {
const fn = (() => 1337) as WrappedFunction;
Object.defineProperty(fn, 'some', {
get(): void {
throw new Error('boom');
},
});
const wrapped = wrap(fn);
expect(wrapped).not.toHaveProperty('some');
});
it('recrusively wraps arguments that are functions', () => {
const fn = (() => 1337) as WrappedFunction;
const fnArgA = (): number => 1337;
const fnArgB = (): number => 1337;
const wrapped = wrap(fn);
wrapped(fnArgA, fnArgB);
expect(fnArgA).toHaveProperty('__sentry_wrapped__');
expect(fnArgB).toHaveProperty('__sentry_wrapped__');
});
it('calls the original function', () => {
const fn = spy();
wrap(fn)(123, 'Rick');
expect(fn.called).toBe(true);
expect(fn.getCalls()[0].args[0]).toBe(123);
expect(fn.getCalls()[0].args[1]).toBe('Rick');
});
it('preserves `this` context for all the calls', () => {
const context = {
fn(): void {
expect(this).toBe(context);
},
eventFn(): void {
return;
},
};
// @ts-ignore eventFn does not have property handleEvent
context.eventFn.handleEvent = function (): void {
expect(this).toBe(context);
};
// eslint-disable-next-line @typescript-eslint/unbound-method
const wrappedFn = wrap(context.fn);
// eslint-disable-next-line @typescript-eslint/unbound-method
const wrappedEventFn = wrap(context.eventFn);
wrappedFn.call(context);
wrappedEventFn.call(context);
});
it('should rethrow caught exceptions', () => {
const fn = (): number => {
throw new Error('boom');
};
const wrapped = wrap(fn);
try {
wrapped();
} catch (error) {
expect((error as Error).message).toBe('boom');
}
});
it('internal flags shouldnt be enumerable', () => {
const fn = (() => 1337) as WrappedFunction;
const wrapped = wrap(fn);
// Shouldn't show up in iteration
expect(Object.keys(fn)).toEqual(expect.not.arrayContaining(['__sentry_original__']));
expect(Object.keys(fn)).toEqual(expect.not.arrayContaining(['__sentry_wrapped__']));
expect(Object.keys(wrapped)).toEqual(expect.not.arrayContaining(['__sentry_original__']));
expect(Object.keys(wrapped)).toEqual(expect.not.arrayContaining(['__sentry_wrapped__']));
// But should be accessible directly
expect(wrapped.__sentry_original__).toBe(fn);
expect(fn.__sentry_wrapped__).toBe(wrapped);
});
});