|
2 | 2 | * @jest-environment jsdom
|
3 | 3 | */
|
4 | 4 |
|
5 |
| -import { dropUndefinedKeys, extractExceptionKeysForMessage, fill, objectify, urlEncode } from '../src/object'; |
| 5 | +import { WrappedFunction } from '@sentry/types'; |
| 6 | +import { |
| 7 | + dropUndefinedKeys, |
| 8 | + extractExceptionKeysForMessage, |
| 9 | + fill, |
| 10 | + objectify, |
| 11 | + urlEncode, |
| 12 | + addNonEnumerableProperty, |
| 13 | + markFunctionWrapped, |
| 14 | +} from '../src/object'; |
6 | 15 | import { testOnlyIfNodeVersionAtLeast } from './testutils';
|
7 | 16 |
|
8 | 17 | describe('fill()', () => {
|
@@ -315,3 +324,95 @@ describe('objectify()', () => {
|
315 | 324 | expect(objectifiedNonPrimtive).toBe(notAPrimitive);
|
316 | 325 | });
|
317 | 326 | });
|
| 327 | + |
| 328 | +describe('addNonEnumerableProperty', () => { |
| 329 | + it('works with a plain object', () => { |
| 330 | + const obj: { foo?: string } = {}; |
| 331 | + addNonEnumerableProperty(obj, 'foo', 'bar'); |
| 332 | + expect(obj.foo).toBe('bar'); |
| 333 | + }); |
| 334 | + |
| 335 | + it('works with a class', () => { |
| 336 | + class MyClass { |
| 337 | + public foo?: string; |
| 338 | + } |
| 339 | + const obj = new MyClass(); |
| 340 | + addNonEnumerableProperty(obj as any, 'foo', 'bar'); |
| 341 | + expect(obj.foo).toBe('bar'); |
| 342 | + }); |
| 343 | + |
| 344 | + it('works with a function', () => { |
| 345 | + const func = jest.fn(); |
| 346 | + addNonEnumerableProperty(func as any, 'foo', 'bar'); |
| 347 | + expect((func as any).foo).toBe('bar'); |
| 348 | + func(); |
| 349 | + expect(func).toHaveBeenCalledTimes(1); |
| 350 | + }); |
| 351 | + |
| 352 | + it('works with an existing property object', () => { |
| 353 | + const obj = { foo: 'before' }; |
| 354 | + addNonEnumerableProperty(obj, 'foo', 'bar'); |
| 355 | + expect(obj.foo).toBe('bar'); |
| 356 | + }); |
| 357 | + |
| 358 | + it('works with an existing readonly property object', () => { |
| 359 | + const obj = { foo: 'before' }; |
| 360 | + |
| 361 | + Object.defineProperty(obj, 'foo', { |
| 362 | + value: 'defined', |
| 363 | + writable: false, |
| 364 | + }); |
| 365 | + |
| 366 | + addNonEnumerableProperty(obj, 'foo', 'bar'); |
| 367 | + expect(obj.foo).toBe('bar'); |
| 368 | + }); |
| 369 | + |
| 370 | + it('does not error with a frozen object', () => { |
| 371 | + const obj = Object.freeze({ foo: 'before' }); |
| 372 | + |
| 373 | + addNonEnumerableProperty(obj, 'foo', 'bar'); |
| 374 | + expect(obj.foo).toBe('before'); |
| 375 | + }); |
| 376 | +}); |
| 377 | + |
| 378 | +describe('markFunctionWrapped', () => { |
| 379 | + it('works with a function', () => { |
| 380 | + const originalFunc = jest.fn(); |
| 381 | + const wrappedFunc = jest.fn(); |
| 382 | + markFunctionWrapped(wrappedFunc, originalFunc); |
| 383 | + |
| 384 | + expect((wrappedFunc as WrappedFunction).__sentry_original__).toBe(originalFunc); |
| 385 | + |
| 386 | + wrappedFunc(); |
| 387 | + |
| 388 | + expect(wrappedFunc).toHaveBeenCalledTimes(1); |
| 389 | + expect(originalFunc).not.toHaveBeenCalled(); |
| 390 | + }); |
| 391 | + |
| 392 | + it('works with a frozen original function', () => { |
| 393 | + const originalFunc = Object.freeze(jest.fn()); |
| 394 | + const wrappedFunc = jest.fn(); |
| 395 | + markFunctionWrapped(wrappedFunc, originalFunc); |
| 396 | + |
| 397 | + expect((wrappedFunc as WrappedFunction).__sentry_original__).toBe(originalFunc); |
| 398 | + |
| 399 | + wrappedFunc(); |
| 400 | + |
| 401 | + expect(wrappedFunc).toHaveBeenCalledTimes(1); |
| 402 | + expect(originalFunc).not.toHaveBeenCalled(); |
| 403 | + }); |
| 404 | + |
| 405 | + it('works with a frozen wrapped function', () => { |
| 406 | + const originalFunc = Object.freeze(jest.fn()); |
| 407 | + const wrappedFunc = Object.freeze(jest.fn()); |
| 408 | + markFunctionWrapped(wrappedFunc, originalFunc); |
| 409 | + |
| 410 | + // Skips adding the property, but also doesn't error |
| 411 | + expect((wrappedFunc as WrappedFunction).__sentry_original__).toBe(undefined); |
| 412 | + |
| 413 | + wrappedFunc(); |
| 414 | + |
| 415 | + expect(wrappedFunc).toHaveBeenCalledTimes(1); |
| 416 | + expect(originalFunc).not.toHaveBeenCalled(); |
| 417 | + }); |
| 418 | +}); |
0 commit comments