@@ -25,7 +25,15 @@ const GLOBAL_MODULE_PATH = execSync('npm root -g').toString().trim();
2525const { name, version } = require ( '../../../package.json' ) ;
2626
2727const { CYPRESS_V10_AND_ABOVE_CONFIG_FILE_EXTENSIONS } = require ( '../../helpers/constants' ) ;
28- const { consoleHolder, API_URL , TEST_OBSERVABILITY_REPORTER } = require ( './constants' ) ;
28+ const { consoleHolder, API_URL , TEST_OBSERVABILITY_REPORTER , TEST_OBSERVABILITY_REPORTER_LOCAL } = require ( './constants' ) ;
29+
30+ const ALLOWED_MODULES = [
31+ 'cypress/package.json' ,
32+ 'mocha/lib/reporters/base.js' ,
33+ 'mocha/lib/utils.js' ,
34+ 'mocha'
35+ ] ;
36+
2937exports . pending_test_uploads = {
3038 count : 0
3139} ;
@@ -715,32 +723,85 @@ exports.getOSDetailsFromSystem = async (product) => {
715723 } ;
716724}
717725
718- exports . requireModule = ( module , internal = false ) => {
719- logger . debug ( `Getting ${ module } from ${ process . cwd ( ) } ` ) ;
720- let local_path = "" ;
721- if ( process . env [ "browserStackCwd" ] ) {
722- local_path = path . join ( process . env [ "browserStackCwd" ] , 'node_modules' , module ) ;
723- } else if ( internal ) {
724- local_path = path . join ( process . cwd ( ) , 'node_modules' , 'browserstack-cypress-cli' , 'node_modules' , module ) ;
725- } else {
726- local_path = path . join ( process . cwd ( ) , 'node_modules' , module ) ;
727- }
728- if ( ! fs . existsSync ( local_path ) ) {
729- logger . debug ( `${ module } doesn\'t exist at ${ process . cwd ( ) } ` ) ;
730- logger . debug ( `Getting ${ module } from ${ GLOBAL_MODULE_PATH } ` ) ;
731-
732- let global_path ;
733- if ( [ 'jest-runner' , 'jest-runtime' ] . includes ( module ) )
734- global_path = path . join ( GLOBAL_MODULE_PATH , 'jest' , 'node_modules' , module ) ;
735- else
736- global_path = path . join ( GLOBAL_MODULE_PATH , module ) ;
737- if ( ! fs . existsSync ( global_path ) ) {
738- throw new Error ( `${ module } doesn't exist.` ) ;
726+ let WORKSPACE_MODULE_PATH ;
727+
728+ exports . requireModule = ( module ) => {
729+ const modulePath = exports . resolveModule ( module ) ;
730+ if ( modulePath . error ) {
731+ throw new Error ( `${ module } doesn't exist.` ) ;
732+ }
733+
734+ return require ( modulePath . path ) ;
735+ } ;
736+
737+ exports . resolveModule = ( module ) => {
738+ if ( ! ALLOWED_MODULES . includes ( module ) ) {
739+ throw new Error ( 'Invalid module name' ) ;
740+ }
741+
742+ if ( WORKSPACE_MODULE_PATH == undefined ) {
743+ try {
744+ WORKSPACE_MODULE_PATH = execSync ( 'npm ls' ) . toString ( ) . trim ( ) ;
745+ WORKSPACE_MODULE_PATH = WORKSPACE_MODULE_PATH . split ( '\n' ) [ 0 ] . split ( ' ' ) [ 1 ] ;
746+ } catch ( e ) {
747+ WORKSPACE_MODULE_PATH = null ;
748+ exports . debug ( `Could not locate npm module path with error ${ e } ` ) ;
739749 }
740- return require ( global_path ) ;
741750 }
742- return require ( local_path ) ;
743- }
751+
752+ /*
753+ Modules will be resolved in the following order,
754+ current working dir > workspaces dir > NODE_PATH env var > global node modules path
755+ */
756+
757+ try {
758+ exports . debug ( 'requireModuleV2' ) ;
759+
760+ return { path : require . resolve ( module ) , foundAt : 'resolve' } ;
761+ } catch ( _ ) {
762+ /* Find from current working directory */
763+ exports . debug ( `Getting ${ module } from ${ process . cwd ( ) } ` ) ;
764+ let local_path = path . join ( process . cwd ( ) , 'node_modules' , module ) ;
765+ if ( ! fs . existsSync ( local_path ) ) {
766+ exports . debug ( `${ module } doesn't exist at ${ process . cwd ( ) } ` ) ;
767+
768+ /* Find from workspaces */
769+ if ( WORKSPACE_MODULE_PATH ) {
770+ exports . debug ( `Getting ${ module } from path ${ WORKSPACE_MODULE_PATH } ` ) ;
771+ let workspace_path = null ;
772+ workspace_path = path . join ( WORKSPACE_MODULE_PATH , 'node_modules' , module ) ;
773+ if ( workspace_path && fs . existsSync ( workspace_path ) ) {
774+ exports . debug ( `Found ${ module } from ${ WORKSPACE_MODULE_PATH } ` ) ;
775+
776+ return { path : workspace_path , foundAt : 'workspaces' } ;
777+ }
778+ }
779+
780+ /* Find from node path */
781+ let node_path = null ;
782+ if ( ! exports . isUndefined ( process . env . NODE_PATH ) ) {
783+ node_path = path . join ( process . env . NODE_PATH , module ) ;
784+ }
785+ if ( node_path && fs . existsSync ( node_path ) ) {
786+ exports . debug ( `Getting ${ module } from ${ process . env . NODE_PATH } ` ) ;
787+
788+ return { path : node_path , foundAt : 'nodePath' } ;
789+ }
790+
791+ /* Find from global node modules path */
792+ exports . debug ( `Getting ${ module } from ${ GLOBAL_MODULE_PATH } ` ) ;
793+
794+ let global_path = path . join ( GLOBAL_MODULE_PATH , module ) ;
795+ if ( ! global_path || ! fs . existsSync ( global_path ) ) {
796+ return { error : 'module_not_found' } ;
797+ }
798+
799+ return { path : global_path , foundAt : 'local' } ;
800+ }
801+
802+ return { path : local_path , foundAt : 'global' } ;
803+ }
804+ } ;
744805
745806const getReRunSpecs = ( rawArgs ) => {
746807 if ( this . isTestObservabilitySession ( ) && this . shouldReRunObservabilityTests ( ) ) {
@@ -763,7 +824,7 @@ const getReRunSpecs = (rawArgs) => {
763824
764825const getLocalSessionReporter = ( ) => {
765826 if ( this . isTestObservabilitySession ( ) && process . env . BS_TESTOPS_JWT ) {
766- return [ '--reporter' , TEST_OBSERVABILITY_REPORTER ] ;
827+ return [ '--reporter' , TEST_OBSERVABILITY_REPORTER_LOCAL ] ;
767828 } else {
768829 return [ ] ;
769830 }
0 commit comments