File tree Expand file tree Collapse file tree 2 files changed +26
-0
lines changed
Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -6,9 +6,12 @@ const fileNamePattern = /(\w*(\.\w*)*)\..*/
66module . exports = function getPluginName ( fn ) {
77 if ( fn . name . length > 0 ) return fn . name
88
9+ const stackTraceLimit = Error . stackTraceLimit
10+ Error . stackTraceLimit = 10
911 try {
1012 throw new Error ( 'anonymous function' )
1113 } catch ( e ) {
14+ Error . stackTraceLimit = stackTraceLimit
1215 return extractPluginName ( e . stack )
1316 }
1417}
Original file line number Diff line number Diff line change @@ -155,6 +155,29 @@ test('should set anonymous function name to file it was called from with a count
155155 t . end ( )
156156} )
157157
158+ test ( 'should set function name if Error.stackTraceLimit is set to 0' , t => {
159+ const stackTraceLimit = Error . stackTraceLimit = 0
160+
161+ const fp = proxyquire ( '../plugin.js' , { stubs : { } } )
162+
163+ const fn = fp ( ( fastify , opts , next ) => {
164+ next ( )
165+ } )
166+
167+ t . equal ( fn [ Symbol . for ( 'plugin-meta' ) ] . name , 'test-auto-0' )
168+ t . equal ( fn [ Symbol . for ( 'fastify.display-name' ) ] , 'test-auto-0' )
169+
170+ const fn2 = fp ( ( fastify , opts , next ) => {
171+ next ( )
172+ } )
173+
174+ t . equal ( fn2 [ Symbol . for ( 'plugin-meta' ) ] . name , 'test-auto-1' )
175+ t . equal ( fn2 [ Symbol . for ( 'fastify.display-name' ) ] , 'test-auto-1' )
176+
177+ Error . stackTraceLimit = stackTraceLimit
178+ t . end ( )
179+ } )
180+
158181test ( 'should set display-name to meta name' , t => {
159182 t . plan ( 2 )
160183
You can’t perform that action at this time.
0 commit comments