/
postgres.test.ts
97 lines (86 loc) · 2.7 KB
/
postgres.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
/* eslint-disable @typescript-eslint/unbound-method */
import { Hub, Scope } from '@sentry/hub';
import { Postgres } from '../../../src/integrations/node/postgres';
import { Span } from '../../../src/span';
class PgClient {
// https://node-postgres.com/api/client#clientquery
public query(_text: unknown, values: unknown, callback?: () => void) {
if (typeof callback === 'function') {
callback();
return;
}
if (typeof values === 'function') {
values();
return;
}
return Promise.resolve();
}
}
// mock for 'pg' / 'pg-native' package
jest.mock('@sentry/utils', () => {
const actual = jest.requireActual('@sentry/utils');
return {
...actual,
loadModule() {
return {
Client: PgClient,
native: {
Client: PgClient,
},
};
},
};
});
describe('setupOnce', () => {
['pg', 'pg-native'].forEach(pgApi => {
const Client: PgClient = new PgClient();
let scope = new Scope();
let parentSpan: Span;
let childSpan: Span;
beforeAll(() => {
(pgApi === 'pg' ? new Postgres() : new Postgres({ usePgNative: true })).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 ${pgApi}'s query method accepting callback as the last argument`, done => {
Client.query('SELECT NOW()', {}, function() {
expect(scope.getSpan).toBeCalled();
expect(parentSpan.startChild).toBeCalledWith({
description: 'SELECT NOW()',
op: 'db',
});
expect(childSpan.finish).toBeCalled();
done();
}) as void;
});
it(`should wrap ${pgApi}'s query method accepting callback as the second argument`, done => {
Client.query('SELECT NOW()', function() {
expect(scope.getSpan).toBeCalled();
expect(parentSpan.startChild).toBeCalledWith({
description: 'SELECT NOW()',
op: 'db',
});
expect(childSpan.finish).toBeCalled();
done();
}) as void;
});
it(`should wrap ${pgApi}'s query method accepting no callback as the last argument but returning promise`, async () => {
await Client.query('SELECT NOW()', null);
expect(scope.getSpan).toBeCalled();
expect(parentSpan.startChild).toBeCalledWith({
description: 'SELECT NOW()',
op: 'db',
});
expect(childSpan.finish).toBeCalled();
});
});
});