@@ -12,6 +12,7 @@ import { Package } from "../Package";
1212import { BuiltinModule } from "../BuiltinModule" ;
1313import {
1414 __instrumentInspectArgs ,
15+ __instrumentModifyArgs ,
1516 __wrapBuiltinExports ,
1617} from "./injectedFunctions" ;
1718import { createTestAgent } from "../../../helpers/createTestAgent" ;
@@ -119,6 +120,7 @@ t.test("it works", async (t) => {
119120t . test ( "it works using injected functions" , async ( t ) => {
120121 let pkgInspectArgsCalled = false ;
121122 let builtinOnRequireCalled = false ;
123+ let pkgModifyArgsCalled = false ;
122124
123125 const pkg = new Package ( "foo" ) ;
124126 pkg . withVersion ( "^1.0.0" ) . addFileInstrumentation ( {
@@ -130,6 +132,10 @@ t.test("it works using injected functions", async (t) => {
130132 inspectArgs : ( ) => {
131133 pkgInspectArgsCalled = true ;
132134 } ,
135+ modifyArgs : ( args ) => {
136+ pkgModifyArgsCalled = true ;
137+ return [ 42 ] ;
138+ } ,
133139 } ,
134140 ] ,
135141 } ) ;
@@ -145,17 +151,96 @@ t.test("it works using injected functions", async (t) => {
145151
146152 t . equal ( pkgInspectArgsCalled , false ) ;
147153 __instrumentInspectArgs ( "foo.bar.js.bazABCDEF.^1.0.0" , [ ] ) ;
154+ __instrumentModifyArgs ( "foo.bar.js.bazABCDEF.^1.0.0" , [ ] ) ;
148155 t . equal ( pkgInspectArgsCalled , false ) ;
156+ t . equal ( pkgModifyArgsCalled , false ) ;
149157 __instrumentInspectArgs ( "foo.bar.js.baz.^1.0.0" , [ ] ) ;
158+ __instrumentModifyArgs ( "foo.bar.js.baz.^1.0.0" , [ ] ) ;
150159 // No agent yet
151160 t . equal ( pkgInspectArgsCalled , false ) ;
161+ t . equal ( pkgModifyArgsCalled , false ) ;
162+
163+ createTestAgent ( ) ;
164+
165+ __instrumentInspectArgs ( "foo.bar.js.bazABCDEF.^1.0.0" , [ ] ) ;
166+ __instrumentModifyArgs ( "foo.bar.js.bazABCDEF.^1.0.0" , [ ] ) ;
167+ t . equal ( pkgInspectArgsCalled , false ) ;
168+ t . equal ( pkgModifyArgsCalled , false ) ;
152169
153- const agent = createTestAgent ( ) ;
154170 __instrumentInspectArgs ( "foo.bar.js.baz.^1.0.0" , [ ] ) ;
155171 t . equal ( pkgInspectArgsCalled , true ) ;
172+ t . same ( __instrumentModifyArgs ( "foo.bar.js.baz.^1.0.0" , [ ] ) , [ 42 ] ) ;
173+ t . equal ( pkgModifyArgsCalled , true ) ;
156174
157175 t . equal ( builtinOnRequireCalled , false ) ;
158176 const wrapped = __wrapBuiltinExports ( "http" , { } ) as any ;
159177 t . equal ( builtinOnRequireCalled , true ) ;
160178 t . equal ( wrapped . test , 42 ) ;
161179} ) ;
180+
181+ t . test ( "modifyArgs always returns a array" , async ( t ) => {
182+ const pkg = new Package ( "foo" ) ;
183+ pkg . withVersion ( "^1.0.0" ) . addFileInstrumentation ( {
184+ path : "xyz.js" ,
185+ functions : [
186+ {
187+ nodeType : "MethodDefinition" ,
188+ name : "abc" ,
189+ modifyArgs : ( args ) => {
190+ return args ;
191+ } ,
192+ } ,
193+ {
194+ nodeType : "MethodDefinition" ,
195+ name : "xyz" ,
196+ // @ts -expect-error Testing invalid input
197+ modifyArgs : ( args ) => {
198+ return undefined ;
199+ } ,
200+ } ,
201+ ] ,
202+ } ) ;
203+
204+ setPackagesToInstrument ( [ pkg ] ) ;
205+ createTestAgent ( ) ;
206+
207+ t . same ( __instrumentModifyArgs ( "foo.xyz.js.abc.^1.0.0" , [ 1 , 2 , 3 ] ) , [ 1 , 2 , 3 ] ) ;
208+ // @ts -expect-error Testing invalid input
209+ t . same ( __instrumentModifyArgs ( "foo.xyz.js.abc.^1.0.0" , undefined ) , [ ] ) ;
210+ t . same (
211+ __instrumentModifyArgs ( "foo.xyz.js.doesnotexist" , [ 1 , 2 , 3 ] ) ,
212+ [ 1 , 2 , 3 ]
213+ ) ;
214+ t . same ( __instrumentModifyArgs ( "foo.xyz.js.xyz.^1.0.0" , [ 1 , 2 , 3 ] ) , [ 1 , 2 , 3 ] ) ;
215+ // @ts -expect-error Testing invalid input
216+ t . same ( __instrumentModifyArgs ( "foo.xyz.js.xyz.^1.0.0" , undefined ) , [ ] ) ;
217+ } ) ;
218+
219+ t . test ( "all injected functions handle errors" , async ( t ) => {
220+ const pkg = new Package ( "foo" ) ;
221+ pkg . withVersion ( "^1.0.0" ) . addFileInstrumentation ( {
222+ path : "dist/test.mjs" ,
223+ functions : [
224+ {
225+ nodeType : "MethodDefinition" ,
226+ name : "abc" ,
227+ inspectArgs : ( ) => {
228+ throw new Error ( "test" ) ;
229+ } ,
230+ modifyArgs : ( ) => {
231+ throw new Error ( "test" ) ;
232+ } ,
233+ modifyReturnValue : ( ) => {
234+ throw new Error ( "test" ) ;
235+ } ,
236+ } ,
237+ ] ,
238+ } ) ;
239+
240+ setPackagesToInstrument ( [ pkg ] ) ;
241+ createTestAgent ( ) ;
242+
243+ __instrumentInspectArgs ( "foo.dist/test.mjs.abc.^1.0.0" , [ ] ) ;
244+ __instrumentModifyArgs ( "foo.dist/test.mjs.abc.^1.0.0" , [ ] ) ;
245+ t . same ( __instrumentModifyArgs ( "foo.dist/test.mjs.abc.^1.0.0" , [ ] ) , [ ] ) ;
246+ } ) ;
0 commit comments