|
| 1 | +/* eslint-disable @typescript-eslint/unbound-method */ |
| 2 | +import { Hub, Scope } from '@sentry/hub'; |
| 3 | + |
| 4 | +import { Mongo } from '../../src/integrations/mongo'; |
| 5 | +import { Span } from '../../src/span'; |
| 6 | + |
| 7 | +class Collection { |
| 8 | + public collectionName: string = 'mockedCollectionName'; |
| 9 | + public dbName: string = 'mockedDbName'; |
| 10 | + public namespace: string = 'mockedNamespace'; |
| 11 | + |
| 12 | + // Method that can have a callback as last argument, or return a promise otherwise. |
| 13 | + insertOne(_doc: unknown, _options: unknown, callback?: () => void) { |
| 14 | + if (typeof callback === 'function') { |
| 15 | + callback(); |
| 16 | + return; |
| 17 | + } |
| 18 | + return Promise.resolve(); |
| 19 | + } |
| 20 | + // Method that has no callback as last argument, and doesnt return promise. |
| 21 | + initializeOrderedBulkOp() { |
| 22 | + return {}; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +jest.mock('@sentry/utils', () => { |
| 27 | + const actual = jest.requireActual('@sentry/utils'); |
| 28 | + return { |
| 29 | + ...actual, |
| 30 | + dynamicRequire() { |
| 31 | + return { |
| 32 | + Collection, |
| 33 | + }; |
| 34 | + }, |
| 35 | + }; |
| 36 | +}); |
| 37 | + |
| 38 | +describe('patchOperation()', () => { |
| 39 | + const doc = { |
| 40 | + name: 'PickleRick', |
| 41 | + answer: 42, |
| 42 | + }; |
| 43 | + const collection: Collection = new Collection(); |
| 44 | + let scope = new Scope(); |
| 45 | + let parentSpan: Span; |
| 46 | + let childSpan: Span; |
| 47 | + |
| 48 | + beforeAll(() => { |
| 49 | + new Mongo({ |
| 50 | + operations: ['insertOne', 'initializeOrderedBulkOp'], |
| 51 | + }).setupOnce( |
| 52 | + () => undefined, |
| 53 | + () => new Hub(undefined, scope), |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + beforeEach(() => { |
| 58 | + scope = new Scope(); |
| 59 | + parentSpan = new Span(); |
| 60 | + childSpan = parentSpan.startChild(); |
| 61 | + jest.spyOn(scope, 'getSpan').mockReturnValueOnce(parentSpan); |
| 62 | + jest.spyOn(parentSpan, 'startChild').mockReturnValueOnce(childSpan); |
| 63 | + jest.spyOn(childSpan, 'finish'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should wrap method accepting callback as the last argument', done => { |
| 67 | + collection.insertOne(doc, {}, function() { |
| 68 | + expect(scope.getSpan).toBeCalled(); |
| 69 | + expect(parentSpan.startChild).toBeCalledWith({ |
| 70 | + data: { |
| 71 | + collectionName: 'mockedCollectionName', |
| 72 | + dbName: 'mockedDbName', |
| 73 | + doc: JSON.stringify(doc), |
| 74 | + namespace: 'mockedNamespace', |
| 75 | + }, |
| 76 | + op: `db`, |
| 77 | + description: 'insertOne', |
| 78 | + }); |
| 79 | + expect(childSpan.finish).toBeCalled(); |
| 80 | + done(); |
| 81 | + }) as void; |
| 82 | + }); |
| 83 | + |
| 84 | + it('should wrap method accepting no callback as the last argument but returning promise', async () => { |
| 85 | + await collection.insertOne(doc, {}); |
| 86 | + expect(scope.getSpan).toBeCalled(); |
| 87 | + expect(parentSpan.startChild).toBeCalledWith({ |
| 88 | + data: { |
| 89 | + collectionName: 'mockedCollectionName', |
| 90 | + dbName: 'mockedDbName', |
| 91 | + doc: JSON.stringify(doc), |
| 92 | + namespace: 'mockedNamespace', |
| 93 | + }, |
| 94 | + op: `db`, |
| 95 | + description: 'insertOne', |
| 96 | + }); |
| 97 | + expect(childSpan.finish).toBeCalled(); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should wrap method accepting no callback as the last argument and not returning promise', () => { |
| 101 | + collection.initializeOrderedBulkOp(); |
| 102 | + expect(scope.getSpan).toBeCalled(); |
| 103 | + expect(parentSpan.startChild).toBeCalledWith({ |
| 104 | + data: { |
| 105 | + collectionName: 'mockedCollectionName', |
| 106 | + dbName: 'mockedDbName', |
| 107 | + namespace: 'mockedNamespace', |
| 108 | + }, |
| 109 | + op: `db`, |
| 110 | + description: 'initializeOrderedBulkOp', |
| 111 | + }); |
| 112 | + expect(childSpan.finish).toBeCalled(); |
| 113 | + }); |
| 114 | +}); |
0 commit comments