11/* eslint-disable deprecation/deprecation */
22/* eslint-disable @typescript-eslint/unbound-method */
33import { Hub , Scope } from '@sentry/core' ;
4- import { logger } from '@sentry/utils' ;
4+ import { logger , loadModule } from '@sentry/utils' ;
55
66import { Integrations , Span } from '../../../src' ;
77import { getTestClient } from '../../testutils' ;
88
99class PgClient {
1010 // 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 ) {
1212 if ( typeof callback === 'function' ) {
13- callback ( ) ;
13+ callback ( null , null ) ;
1414 return ;
1515 }
1616
@@ -25,25 +25,28 @@ class PgClient {
2525
2626// Jest mocks get hoisted. vars starting with `mock` are hoisted before imports.
2727/* eslint-disable no-var */
28- var mockClient = PgClient ;
28+ var mockModule = {
29+ Client : PgClient ,
30+ native : {
31+ Client : PgClient ,
32+ } ,
33+ } ;
2934
3035// mock for 'pg' / 'pg-native' package
3136jest . mock ( '@sentry/utils' , ( ) => {
3237 const actual = jest . requireActual ( '@sentry/utils' ) ;
3338 return {
3439 ...actual ,
35- loadModule ( ) {
36- return {
37- Client : mockClient ,
38- native : {
39- Client : mockClient ,
40- } ,
41- } ;
42- } ,
40+ loadModule : jest . fn ( ( ) => mockModule ) ,
4341 } ;
4442} ) ;
4543
4644describe ( 'setupOnce' , ( ) => {
45+ beforeEach ( ( ) => {
46+ jest . clearAllMocks ( ) ;
47+ jest . resetAllMocks ( ) ;
48+ } ) ;
49+
4750 [ 'pg' , 'pg-native' ] . forEach ( pgApi => {
4851 const Client : PgClient = new PgClient ( ) ;
4952 let scope = new Scope ( ) ;
@@ -127,4 +130,19 @@ describe('setupOnce', () => {
127130
128131 expect ( loggerLogSpy ) . toBeCalledWith ( 'Postgres Integration is skipped because of instrumenter configuration.' ) ;
129132 } ) ;
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+ } ) ;
130148} ) ;
0 commit comments