import * as utils from '../src/utils' ; import * as assert from 'assert' ; import { ExpressInstrumentationConfig , ExpressLayer } from '../src/types' ; import { ExpressLayerType } from '../src/enums/ExpressLayerType' ; import { AttributeNames } from '../src/enums/AttributeNames' ; describe ( 'Utils' , ( ) => { describe ( 'isLayerIgnored()' , ( ) => { it ( 'should not fail with invalid config' , ( ) => { assert . doesNotThrow ( ( ) => utils . isLayerIgnored ( '' , ExpressLayerType . MIDDLEWARE ) ) ; assert . doesNotThrow ( ( ) => utils . isLayerIgnored ( '' , ExpressLayerType . MIDDLEWARE , { } as ExpressInstrumentationConfig ) ) ; assert . doesNotThrow ( ( ) => utils . isLayerIgnored ( '' , ExpressLayerType . MIDDLEWARE , { ignoreLayersType : { } , } as ExpressInstrumentationConfig ) ) ; assert . doesNotThrow ( ( ) => utils . isLayerIgnored ( '' , ExpressLayerType . MIDDLEWARE , { ignoreLayersType : { } , ignoreLayers : { } , } as ExpressInstrumentationConfig ) ) ; } ) ; it ( 'should ignore based on type' , ( ) => { assert . deepEqual ( utils . isLayerIgnored ( '' , ExpressLayerType . MIDDLEWARE , { ignoreLayersType : [ ExpressLayerType . MIDDLEWARE ] , } ) , true ) ; assert . deepEqual ( utils . isLayerIgnored ( '' , ExpressLayerType . ROUTER , { ignoreLayersType : [ ExpressLayerType . MIDDLEWARE ] , } ) , false ) ; } ) ; it ( 'should ignore based on the name' , ( ) => { assert . deepEqual ( utils . isLayerIgnored ( 'bodyParser' , ExpressLayerType . MIDDLEWARE , { ignoreLayers : [ 'bodyParser' ] , } ) , true ) ; assert . deepEqual ( utils . isLayerIgnored ( 'bodyParser' , ExpressLayerType . MIDDLEWARE , { ignoreLayers : [ ( name ) => name === 'bodyParser' ] , } ) , true ) ; assert . deepEqual ( utils . isLayerIgnored ( 'bodyParser' , ExpressLayerType . MIDDLEWARE , { ignoreLayers : [ /bodyParser/ ] , } ) , true ) ; assert . deepEqual ( utils . isLayerIgnored ( 'test' , ExpressLayerType . ROUTER , { ignoreLayers : [ 'bodyParser' ] , } ) , false ) ; } ) ; } ) ; describe ( 'getLayerMetadata()' , ( ) => { it ( 'should return router metadata' , ( ) => { assert . deepEqual ( utils . getLayerMetadata ( { name : 'router' , } as ExpressLayer , '/test' ) , { attributes : { [ AttributeNames . EXPRESS_NAME ] : '/test' , [ AttributeNames . EXPRESS_TYPE ] : 'router' , } , name : 'router - /test' , } ) ; } ) ; it ( 'should return request handler metadata' , ( ) => { assert . deepEqual ( utils . getLayerMetadata ( { name : 'bound dispatch' , } as ExpressLayer , '/:id' ) , { attributes : { [ AttributeNames . EXPRESS_NAME ] : '/:id' , [ AttributeNames . EXPRESS_TYPE ] : 'request_handler' , } , name : 'request handler' , } ) ; } ) ; it ( 'should return middleware metadata' , ( ) => { assert . deepEqual ( utils . getLayerMetadata ( { name : 'bodyParser' , } as ExpressLayer ) , { attributes : { [ AttributeNames . EXPRESS_NAME ] : 'bodyParser' , [ AttributeNames . EXPRESS_TYPE ] : 'middleware' , } , name : 'middleware - bodyParser' , } ) ; } ) ; } ) ; } ) ;