-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathmongo.test.ts
135 lines (120 loc) · 3.85 KB
/
mongo.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* eslint-disable @typescript-eslint/unbound-method */
import { Hub, Scope } from '@sentry/core';
import { logger } from '@sentry/utils';
import { Span } from '../../../src';
import { Mongo } from '../../../src/node/integrations/mongo';
import { getTestClient } from '../../testutils';
class Collection {
public collectionName: string = 'mockedCollectionName';
public dbName: string = 'mockedDbName';
public namespace: string = 'mockedNamespace';
// Method that can have a callback as last argument, or return a promise otherwise.
public insertOne(_doc: unknown, _options: unknown, callback?: () => void) {
if (typeof callback === 'function') {
callback();
return;
}
return Promise.resolve();
}
// Method that has no callback as last argument, and doesnt return promise.
public initializeOrderedBulkOp() {
return {};
}
}
// Jest mocks get hoisted. vars starting with `mock` are hoisted before imports.
/* eslint-disable no-var */
var mockCollection = Collection;
jest.mock('@sentry/utils', () => {
const actual = jest.requireActual('@sentry/utils');
return {
...actual,
loadModule() {
return {
Collection: mockCollection,
};
},
};
});
describe('patchOperation()', () => {
const doc = {
name: 'PickleRick',
answer: 42,
};
const collection: Collection = new Collection();
let scope = new Scope();
let parentSpan: Span;
let childSpan: Span;
beforeAll(() => {
new Mongo({
operations: ['insertOne', 'initializeOrderedBulkOp'],
}).setupOnce(
() => undefined,
() => new Hub(undefined, scope),
);
});
beforeEach(() => {
scope = new Scope();
parentSpan = new Span();
childSpan = parentSpan.startChild();
jest.spyOn(scope, 'getSpan').mockReturnValueOnce(parentSpan);
jest.spyOn(parentSpan, 'startChild').mockReturnValueOnce(childSpan);
jest.spyOn(childSpan, 'finish');
});
it('should wrap method accepting callback as the last argument', done => {
collection.insertOne(doc, {}, function () {
expect(scope.getSpan).toBeCalled();
expect(parentSpan.startChild).toBeCalledWith({
data: {
collectionName: 'mockedCollectionName',
dbName: 'mockedDbName',
doc: JSON.stringify(doc),
namespace: 'mockedNamespace',
},
op: 'db',
description: 'insertOne',
});
expect(childSpan.finish).toBeCalled();
done();
}) as void;
});
it('should wrap method accepting no callback as the last argument but returning promise', async () => {
await collection.insertOne(doc, {});
expect(scope.getSpan).toBeCalled();
expect(parentSpan.startChild).toBeCalledWith({
data: {
collectionName: 'mockedCollectionName',
dbName: 'mockedDbName',
doc: JSON.stringify(doc),
namespace: 'mockedNamespace',
},
op: 'db',
description: 'insertOne',
});
expect(childSpan.finish).toBeCalled();
});
it('should wrap method accepting no callback as the last argument and not returning promise', () => {
collection.initializeOrderedBulkOp();
expect(scope.getSpan).toBeCalled();
expect(parentSpan.startChild).toBeCalledWith({
data: {
collectionName: 'mockedCollectionName',
dbName: 'mockedDbName',
namespace: 'mockedNamespace',
},
op: 'db',
description: 'initializeOrderedBulkOp',
});
expect(childSpan.finish).toBeCalled();
});
it("doesn't attach when using otel instrumenter", () => {
const loggerLogSpy = jest.spyOn(logger, 'log');
const client = getTestClient({ instrumenter: 'otel' });
const hub = new Hub(client);
const integration = new Mongo();
integration.setupOnce(
() => {},
() => hub,
);
expect(loggerLogSpy).toBeCalledWith('Mongo Integration is skipped because of instrumenter configuration.');
});
});