|
| 1 | +let loggerFactory; |
| 2 | +const scope: any = {}; |
| 3 | +const withScope = jest.fn(fn => |
| 4 | + fn({ |
| 5 | + setContext: (key: string, val: any) => { |
| 6 | + scope.context = { [key]: val }; |
| 7 | + }, |
| 8 | + setExtras: (val: any) => { |
| 9 | + scope.extras = val; |
| 10 | + }, |
| 11 | + setLevel: (level: any) => { |
| 12 | + scope.level = level; |
| 13 | + }, |
| 14 | + }) |
| 15 | +); |
| 16 | + |
| 17 | +const createCapture = (cb = () => {}) => data => { |
| 18 | + cb(); |
| 19 | + return { data, scope }; |
| 20 | +}; |
| 21 | + |
| 22 | +const captureException = jest.fn(createCapture()); |
| 23 | +const captureMessage = jest.fn(createCapture()); |
| 24 | +const init = jest.fn(); |
| 25 | + |
| 26 | +describe('sentry mocked', () => { |
| 27 | + beforeAll(() => { |
| 28 | + jest.mock('@sentry/node', () => { |
| 29 | + return { |
| 30 | + captureException, |
| 31 | + captureMessage, |
| 32 | + withScope, |
| 33 | + init, |
| 34 | + Severity: { |
| 35 | + Debug: 'debug', |
| 36 | + Info: 'info', |
| 37 | + Warning: 'warning', |
| 38 | + Error: 'error', |
| 39 | + Critical: 'critical', |
| 40 | + }, |
| 41 | + }; |
| 42 | + }); |
| 43 | + loggerFactory = require('..').default; |
| 44 | + }); |
| 45 | + beforeEach(() => { |
| 46 | + captureException.mockReset(); |
| 47 | + captureMessage.mockReset(); |
| 48 | + }); |
| 49 | + test('can create logger with options', () => { |
| 50 | + expect(() => loggerFactory()).not.toThrowError(); |
| 51 | + expect(() => loggerFactory({ sentry: true })).not.toThrowError(); |
| 52 | + expect(init).not.toHaveBeenCalled(); |
| 53 | + expect(() => loggerFactory({ sentry: 'dummy' })).not.toThrowError(); |
| 54 | + expect(init.mock.calls[0]).toMatchInlineSnapshot(` |
| 55 | + Array [ |
| 56 | + Object { |
| 57 | + "dsn": "dummy", |
| 58 | + }, |
| 59 | + ] |
| 60 | + `); |
| 61 | + }); |
| 62 | + |
| 63 | + test('sentry captureMessage is called with correct scope', async () => { |
| 64 | + await new Promise((resolve, reject) => { |
| 65 | + const logger = loggerFactory({ |
| 66 | + sentry: 'DSN', |
| 67 | + }); |
| 68 | + captureMessage.mockImplementation(createCapture(resolve)); |
| 69 | + logger.info('Foo'); |
| 70 | + }); |
| 71 | + expect(captureMessage).toHaveBeenCalledTimes(1); |
| 72 | + expect(captureException).not.toHaveBeenCalled(); |
| 73 | + expect(captureMessage.mock.calls[0]).toMatchInlineSnapshot(` |
| 74 | + Array [ |
| 75 | + "Foo", |
| 76 | + ] |
| 77 | + `); |
| 78 | + expect(captureMessage.mock.results[0].value).toMatchInlineSnapshot(` |
| 79 | + Object { |
| 80 | + "data": "Foo", |
| 81 | + "scope": Object { |
| 82 | + "extras": Object { |
| 83 | + "level": 30, |
| 84 | + "message": "Foo", |
| 85 | + "v": 1, |
| 86 | + }, |
| 87 | + "level": "info", |
| 88 | + }, |
| 89 | + } |
| 90 | + `); |
| 91 | + }); |
| 92 | + |
| 93 | + test('sentry captureException with stack and correct levels', async () => { |
| 94 | + await new Promise((resolve, reject) => { |
| 95 | + const logger = loggerFactory({ |
| 96 | + sentry: 'DSN', |
| 97 | + }); |
| 98 | + captureException.mockReset(); |
| 99 | + captureException.mockImplementation(createCapture(resolve)); |
| 100 | + logger.error(new Error()); |
| 101 | + }); |
| 102 | + expect(captureException).toHaveBeenCalledTimes(1); |
| 103 | + expect(captureMessage).not.toHaveBeenCalled(); |
| 104 | + expect(captureException.mock.results[0].value).toMatchObject({ |
| 105 | + data: expect.any(Error), |
| 106 | + scope: { |
| 107 | + level: 'error', |
| 108 | + }, |
| 109 | + }); |
| 110 | + }); |
| 111 | +}); |
0 commit comments