|
| 1 | +import { getSentryCarrier } from '../../src/carrier'; |
| 2 | +import { SDK_VERSION } from '../../src/version'; |
| 3 | + |
| 4 | +describe('getSentryCarrier', () => { |
| 5 | + describe('base case (one SDK)', () => { |
| 6 | + it('populates the default sentry carrier object if it does not exist', () => { |
| 7 | + const globalObject = {}; |
| 8 | + const sentryCarrier = getSentryCarrier(globalObject); |
| 9 | + |
| 10 | + expect(sentryCarrier).toEqual({ |
| 11 | + extensions: {}, |
| 12 | + }); |
| 13 | + |
| 14 | + expect(globalObject).toEqual({ |
| 15 | + __SENTRY__: { |
| 16 | + version: SDK_VERSION, |
| 17 | + [SDK_VERSION]: { |
| 18 | + extensions: {}, |
| 19 | + }, |
| 20 | + }, |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + it('returns the existing sentry carrier object if it already exists', () => { |
| 25 | + const originalGlobalObject = { |
| 26 | + __SENTRY__: { |
| 27 | + version: SDK_VERSION, |
| 28 | + [SDK_VERSION]: { |
| 29 | + integrations: [() => {}], |
| 30 | + }, |
| 31 | + }, |
| 32 | + }; |
| 33 | + |
| 34 | + const globalObject = { ...originalGlobalObject }; |
| 35 | + // @ts-expect-error - TS complains because the object spread makes the version key become type string |
| 36 | + const sentryCarrier = getSentryCarrier(globalObject); |
| 37 | + |
| 38 | + expect(sentryCarrier).toEqual({ |
| 39 | + integrations: [expect.any(Function)], |
| 40 | + }); |
| 41 | + |
| 42 | + expect(globalObject).toStrictEqual(originalGlobalObject); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('multiple (older) SDKs', () => { |
| 47 | + it("returns the version of the sentry carrier object of the SDK's version rather than the one set in .version", () => { |
| 48 | + const sentryCarrier = getSentryCarrier({ |
| 49 | + __SENTRY__: { |
| 50 | + version: '8.0.0' as const, // another SDK set this |
| 51 | + '8.0.0': { |
| 52 | + // and this object |
| 53 | + extensions: {}, |
| 54 | + }, |
| 55 | + [SDK_VERSION]: { |
| 56 | + integrations: [() => {}], |
| 57 | + }, |
| 58 | + // @ts-expect-error - this is just a test object, no need to pass a hub |
| 59 | + hub: {}, |
| 60 | + }, |
| 61 | + }); |
| 62 | + |
| 63 | + expect(sentryCarrier).toEqual({ |
| 64 | + integrations: [expect.any(Function)], |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + it("doesn't overwrite the .version property if it's already set and creates a new global sentry carrier for the SDK version if not set yet", () => { |
| 69 | + const globalObject = { |
| 70 | + __SENTRY__: { |
| 71 | + version: '8.0.0' as const, |
| 72 | + '8.0.0': { |
| 73 | + // and this object |
| 74 | + integrations: [() => {}], |
| 75 | + }, |
| 76 | + }, |
| 77 | + }; |
| 78 | + |
| 79 | + const sentryCarrier = getSentryCarrier(globalObject); |
| 80 | + |
| 81 | + expect(sentryCarrier).toEqual({ |
| 82 | + extensions: {}, |
| 83 | + }); |
| 84 | + |
| 85 | + expect(globalObject).toEqual({ |
| 86 | + __SENTRY__: { |
| 87 | + version: '8.0.0', |
| 88 | + '8.0.0': { |
| 89 | + integrations: [expect.any(Function)], |
| 90 | + }, |
| 91 | + [SDK_VERSION]: { |
| 92 | + extensions: {}, |
| 93 | + }, |
| 94 | + }, |
| 95 | + }); |
| 96 | + }); |
| 97 | + }); |
| 98 | +}); |
0 commit comments