/
mongo.test.ts
114 lines (104 loc) · 3.19 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
/* eslint-disable @typescript-eslint/unbound-method */
import { Hub, Scope } from '@sentry/hub';
import { Mongo } from '../../../src/integrations/node/mongo';
import { Span } from '../../../src/span';
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.mock('@sentry/utils', () => {
const actual = jest.requireActual('@sentry/utils');
return {
...actual,
loadModule() {
return {
Collection,
};
},
};
});
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();
});
});