@@ -162,8 +162,8 @@ const validate = (bsConfig, args) => {
162162
163163 if ( ! bsConfig . run_settings ) reject ( Constants . validationMessages . EMPTY_RUN_SETTINGS ) ;
164164
165- if ( ! bsConfig . run_settings . cypress_proj_dir && ! bsConfig . run_settings . userProvidedCypessConfigFile ) {
166- reject ( Constants . validationMessages . EMPTY_CYPRESS_PROJ_DIR ) ;
165+ if ( ! bsConfig . run_settings . cypressConfigFilePath && ! bsConfig . run_settings . userProvidedCypessConfigFile ) {
166+ reject ( Constants . validationMessages . EMPTY_CYPRESS_CONFIG_FILE ) ;
167167 }
168168
169169 // validate parallels specified in browserstack.json if parallels are not specified via arguments
@@ -189,25 +189,39 @@ const validate = (bsConfig, args) => {
189189 // validate if config file provided exists or not when cypress_config_file provided
190190 // validate the cypressProjectDir key otherwise.
191191 let cypressConfigFilePath = bsConfig . run_settings . cypressConfigFilePath ;
192- let cypressJson = { } ;
192+ let cypressConfigFile = { } ;
193193
194194 logger . debug ( `Checking for cypress config file at ${ cypressConfigFilePath } ` ) ;
195195 if ( ! fs . existsSync ( cypressConfigFilePath ) && bsConfig . run_settings . cypress_config_filename !== 'false' ) reject ( Constants . validationMessages . INVALID_CYPRESS_CONFIG_FILE ) ;
196196
197- logger . debug ( "Validating cypress.json" ) ;
198- try {
199- if ( bsConfig . run_settings . cypress_config_filename !== 'false' ) {
200- let cypressJsonContent = fs . readFileSync ( cypressConfigFilePath ) ;
201- cypressJson = JSON . parse ( cypressJsonContent ) ;
197+ if ( bsConfig . run_settings . cypressTestSuiteType === Constants . CYPRESS_V10_AND_ABOVE_TYPE ) {
198+ logger . debug ( `Validating ${ bsConfig . run_settings . cypress_config_filename } ` ) ;
199+ // TODO: add validations for cypress_config_filename
200+ } else {
201+ logger . debug ( "Validating cypress.json" ) ;
202+ try {
203+ if ( bsConfig . run_settings . cypress_config_filename !== 'false' ) {
204+
205+ if ( bsConfig . run_settings . cypressTestSuiteType === Constants . CYPRESS_V10_AND_ABOVE_TYPE ) {
206+ if ( cypressConfigFilePath . endsWith ( "cypress.config.js" ) ) {
207+ cypressConfigFile = require ( cypressConfigFilePath ) ;
208+ } else {
209+ cypressConfigFile = { } ;
210+ }
211+ } else {
212+ let cypressJsonContent = fs . readFileSync ( cypressConfigFilePath ) ;
213+ cypressConfigFile = JSON . parse ( cypressJsonContent ) ;
214+ }
202215
203- // Cypress Json Base Url & Local true check
204- if ( ! Utils . isUndefined ( cypressJson . baseUrl ) && cypressJson . baseUrl . includes ( "localhost" ) && ! Utils . getLocalFlag ( bsConfig . connection_settings ) ) reject ( Constants . validationMessages . LOCAL_NOT_SET . replace ( "<baseUrlValue>" , cypressJson . baseUrl ) ) ;
216+ // Cypress Json Base Url & Local true check
217+ if ( ! Utils . isUndefined ( cypressConfigFile . baseUrl ) && cypressConfigFile . baseUrl . includes ( "localhost" ) && ! Utils . getLocalFlag ( bsConfig . connection_settings ) ) reject ( Constants . validationMessages . LOCAL_NOT_SET . replace ( "<baseUrlValue>" , cypressConfigFile . baseUrl ) ) ;
205218
206- // Detect if the user is not using the right directory structure, and throw an error
207- if ( ! Utils . isUndefined ( cypressJson . integrationFolder ) && ! Utils . isCypressProjDirValid ( bsConfig . run_settings . cypressProjectDir , cypressJson . integrationFolder ) ) reject ( Constants . validationMessages . INCORRECT_DIRECTORY_STRUCTURE ) ;
219+ // Detect if the user is not using the right directory structure, and throw an error
220+ if ( ! Utils . isUndefined ( cypressConfigFile . integrationFolder ) && ! Utils . isCypressProjDirValid ( bsConfig . run_settings . cypressProjectDir , cypressConfigFile . integrationFolder ) ) reject ( Constants . validationMessages . INCORRECT_DIRECTORY_STRUCTURE ) ;
221+ }
222+ } catch ( error ) {
223+ reject ( Constants . validationMessages . INVALID_CYPRESS_JSON )
208224 }
209- } catch ( error ) {
210- reject ( Constants . validationMessages . INVALID_CYPRESS_JSON )
211225 }
212226
213227 //check if home_directory is present or not in user run_settings
@@ -231,6 +245,24 @@ const validate = (bsConfig, args) => {
231245 addCypressZipStartLocation ( bsConfig . run_settings ) ;
232246 }
233247
248+ // check if two config files are present at the same location
249+ let cypressFileDirectory = path . dirname ( path . resolve ( bsConfig . run_settings . cypressConfigFilePath ) ) ;
250+ let listOfFiles = fs . readdirSync ( cypressFileDirectory ) ;
251+ let configFilesPresent = [ ] ;
252+ for ( const possibleCypressFileName of Constants . CYPRESS_CONFIG_FILE_NAMES ) {
253+ if ( listOfFiles . includes ( possibleCypressFileName ) ) {
254+ configFilesPresent . push ( possibleCypressFileName ) ;
255+ }
256+ }
257+
258+ if ( configFilesPresent . length === 0 && bsConfig . run_settings . cypress_config_filename !== 'false' ) {
259+ reject ( Constants . validationMessages . CYPRESS_CONFIG_FILE_NOT_FOUND . replace ( '<location>' , cypressFileDirectory ) ) ;
260+ }
261+ if ( configFilesPresent . length > 1 && bsConfig . run_settings . cypress_config_filename !== 'false' ) {
262+ logger . warn ( `We found the following cypress config files ${ configFilesPresent . join ( ', ' ) } at this location: ${ cypressFileDirectory } ` ) ;
263+ reject ( Constants . validationMessages . MORE_THAN_ONE_CYPRESS_CONFIG_FILE_FOUND ) ;
264+ }
265+
234266 if ( ! Utils . isUndefined ( bsConfig . run_settings . spec_timeout ) ) {
235267 if ( Utils . isPositiveInteger ( bsConfig . run_settings . spec_timeout . toString ( ) . trim ( ) ) ) {
236268 if ( Number ( bsConfig . run_settings . spec_timeout ) > Constants . SPEC_TIMEOUT_LIMIT ) {
@@ -257,7 +289,7 @@ const validate = (bsConfig, args) => {
257289 if ( ! Utils . isUndefined ( bsConfig . run_settings . nodeVersion ) && typeof ( bsConfig . run_settings . nodeVersion ) === 'string' && ! bsConfig . run_settings . nodeVersion . match ( / ^ ( \d + \. ) ? ( \d + \. ) ? ( \* | \d + ) $ / ) )
258290 logger . warn ( Constants . validationMessages . NODE_VERSION_PARSING_ERROR ) ;
259291
260- resolve ( cypressJson ) ;
292+ resolve ( cypressConfigFile ) ;
261293 } ) ;
262294}
263295
0 commit comments