|
| 1 | +import type { Envelope, EventEnvelope } from '@sentry/types'; |
| 2 | +import { createEnvelope, logger } from '@sentry/utils'; |
| 3 | +import * as http from 'http'; |
| 4 | + |
| 5 | +import { NodeClient } from '../../src'; |
| 6 | +import { Spotlight } from '../../src/integrations'; |
| 7 | +import { getDefaultNodeClientOptions } from '../helper/node-client-options'; |
| 8 | + |
| 9 | +describe('Spotlight', () => { |
| 10 | + const loggerSpy = jest.spyOn(logger, 'warn'); |
| 11 | + |
| 12 | + afterEach(() => { |
| 13 | + loggerSpy.mockClear(); |
| 14 | + jest.clearAllMocks(); |
| 15 | + }); |
| 16 | + |
| 17 | + const options = getDefaultNodeClientOptions(); |
| 18 | + const client = new NodeClient(options); |
| 19 | + |
| 20 | + it('has a name and id', () => { |
| 21 | + const integration = new Spotlight(); |
| 22 | + expect(integration.name).toEqual('Spotlight'); |
| 23 | + expect(Spotlight.id).toEqual('Spotlight'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('registers a callback on the `beforeEnvelope` hook', () => { |
| 27 | + const clientWithSpy = { |
| 28 | + ...client, |
| 29 | + on: jest.fn(), |
| 30 | + }; |
| 31 | + const integration = new Spotlight(); |
| 32 | + // @ts-expect-error - this is fine in tests |
| 33 | + integration.setup(clientWithSpy); |
| 34 | + expect(clientWithSpy.on).toHaveBeenCalledWith('beforeEnvelope', expect.any(Function)); |
| 35 | + }); |
| 36 | + |
| 37 | + it('sends an envelope POST request to the sidecar url', () => { |
| 38 | + const httpSpy = jest.spyOn(http, 'request').mockImplementationOnce(() => { |
| 39 | + return { |
| 40 | + on: jest.fn(), |
| 41 | + write: jest.fn(), |
| 42 | + end: jest.fn(), |
| 43 | + } as any; |
| 44 | + }); |
| 45 | + |
| 46 | + let callback: (envelope: Envelope) => void = () => {}; |
| 47 | + const clientWithSpy = { |
| 48 | + ...client, |
| 49 | + on: jest.fn().mockImplementationOnce((_, cb) => (callback = cb)), |
| 50 | + }; |
| 51 | + |
| 52 | + const integration = new Spotlight(); |
| 53 | + // @ts-expect-error - this is fine in tests |
| 54 | + integration.setup(clientWithSpy); |
| 55 | + |
| 56 | + const envelope = createEnvelope<EventEnvelope>({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' }, [ |
| 57 | + [{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }], |
| 58 | + ]); |
| 59 | + |
| 60 | + callback(envelope); |
| 61 | + |
| 62 | + expect(httpSpy).toHaveBeenCalledWith( |
| 63 | + { |
| 64 | + headers: { |
| 65 | + 'Content-Type': 'application/x-sentry-envelope', |
| 66 | + }, |
| 67 | + hostname: 'localhost', |
| 68 | + method: 'POST', |
| 69 | + path: '/stream', |
| 70 | + port: '8969', |
| 71 | + }, |
| 72 | + expect.any(Function), |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + describe('no-ops if', () => { |
| 77 | + it('an invalid URL is passed', () => { |
| 78 | + const integration = new Spotlight({ sidecarUrl: 'invalid-url' }); |
| 79 | + integration.setup(client); |
| 80 | + expect(loggerSpy).toHaveBeenCalledWith(expect.stringContaining('Invalid sidecar URL: invalid-url')); |
| 81 | + }); |
| 82 | + |
| 83 | + it("the client doesn't support life cycle hooks", () => { |
| 84 | + const integration = new Spotlight({ sidecarUrl: 'http://mylocalhost:8969' }); |
| 85 | + const clientWithoutHooks = { ...client }; |
| 86 | + // @ts-expect-error - this is fine in tests |
| 87 | + delete client.on; |
| 88 | + // @ts-expect-error - this is fine in tests |
| 89 | + integration.setup(clientWithoutHooks); |
| 90 | + expect(loggerSpy).toHaveBeenCalledWith(expect.stringContaining(' missing method on SDK client (`client.on`)')); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + it('warns if the NODE_ENV variable doesn\'t equal "development"', () => { |
| 95 | + const oldEnvValue = process.env.NODE_ENV; |
| 96 | + process.env.NODE_ENV = 'production'; |
| 97 | + |
| 98 | + const integration = new Spotlight({ sidecarUrl: 'http://localhost:8969' }); |
| 99 | + integration.setup(client); |
| 100 | + |
| 101 | + expect(loggerSpy).toHaveBeenCalledWith( |
| 102 | + expect.stringContaining("It seems you're not in dev mode. Do you really want to have Spoltight enabled?"), |
| 103 | + ); |
| 104 | + |
| 105 | + process.env.NODE_ENV = oldEnvValue; |
| 106 | + }); |
| 107 | + |
| 108 | + it('doesn\'t warn if the NODE_ENV variable equals "development"', () => { |
| 109 | + const oldEnvValue = process.env.NODE_ENV; |
| 110 | + process.env.NODE_ENV = 'development'; |
| 111 | + |
| 112 | + const integration = new Spotlight({ sidecarUrl: 'http://localhost:8969' }); |
| 113 | + integration.setup(client); |
| 114 | + |
| 115 | + expect(loggerSpy).not.toHaveBeenCalledWith( |
| 116 | + expect.stringContaining("It seems you're not in dev mode. Do you really want to have Spoltight enabled?"), |
| 117 | + ); |
| 118 | + |
| 119 | + process.env.NODE_ENV = oldEnvValue; |
| 120 | + }); |
| 121 | +}); |
0 commit comments