1
1
/* eslint-disable deprecation/deprecation */
2
2
/* eslint-disable @typescript-eslint/unbound-method */
3
3
import { Hub , Scope } from '@sentry/core' ;
4
- import { logger } from '@sentry/utils' ;
4
+ import { logger , loadModule } from '@sentry/utils' ;
5
5
6
6
import { Integrations , Span } from '../../../src' ;
7
7
import { getTestClient } from '../../testutils' ;
8
8
9
9
class PgClient {
10
10
// https://node-postgres.com/api/client#clientquery
11
- public query ( _text : unknown , values : unknown , callback ?: ( ) => void ) {
11
+ public query ( _text : unknown , values : unknown , callback ?: ( err : unknown , result : unknown ) => void ) {
12
12
if ( typeof callback === 'function' ) {
13
- callback ( ) ;
13
+ callback ( null , null ) ;
14
14
return ;
15
15
}
16
16
@@ -25,25 +25,28 @@ class PgClient {
25
25
26
26
// Jest mocks get hoisted. vars starting with `mock` are hoisted before imports.
27
27
/* eslint-disable no-var */
28
- var mockClient = PgClient ;
28
+ var mockModule = {
29
+ Client : PgClient ,
30
+ native : {
31
+ Client : PgClient ,
32
+ } ,
33
+ } ;
29
34
30
35
// mock for 'pg' / 'pg-native' package
31
36
jest . mock ( '@sentry/utils' , ( ) => {
32
37
const actual = jest . requireActual ( '@sentry/utils' ) ;
33
38
return {
34
39
...actual ,
35
- loadModule ( ) {
36
- return {
37
- Client : mockClient ,
38
- native : {
39
- Client : mockClient ,
40
- } ,
41
- } ;
42
- } ,
40
+ loadModule : jest . fn ( ( ) => mockModule ) ,
43
41
} ;
44
42
} ) ;
45
43
46
44
describe ( 'setupOnce' , ( ) => {
45
+ beforeEach ( ( ) => {
46
+ jest . clearAllMocks ( ) ;
47
+ jest . resetAllMocks ( ) ;
48
+ } ) ;
49
+
47
50
[ 'pg' , 'pg-native' ] . forEach ( pgApi => {
48
51
const Client : PgClient = new PgClient ( ) ;
49
52
let scope = new Scope ( ) ;
@@ -127,4 +130,19 @@ describe('setupOnce', () => {
127
130
128
131
expect ( loggerLogSpy ) . toBeCalledWith ( 'Postgres Integration is skipped because of instrumenter configuration.' ) ;
129
132
} ) ;
133
+
134
+ it ( 'does not attempt resolution when module is passed directly' , async ( ) => {
135
+ const scope = new Scope ( ) ;
136
+ jest . spyOn ( scope , 'getSpan' ) . mockReturnValueOnce ( new Span ( ) ) ;
137
+
138
+ new Integrations . Postgres ( { module : mockModule } ) . setupOnce (
139
+ ( ) => undefined ,
140
+ ( ) => new Hub ( undefined , scope ) ,
141
+ ) ;
142
+
143
+ await new PgClient ( ) . query ( 'SELECT NOW()' , null ) ;
144
+
145
+ expect ( loadModule ) . not . toBeCalled ( ) ;
146
+ expect ( scope . getSpan ) . toBeCalled ( ) ;
147
+ } ) ;
130
148
} ) ;
0 commit comments