Skip to content

Commit 08f83f5

Browse files
committed
add code changes for cyp 10.3.0
1 parent ed58e18 commit 08f83f5

File tree

7 files changed

+105
-26
lines changed

7 files changed

+105
-26
lines changed

bin/commands/runs.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ module.exports = function run(args, rawArgs) {
6161
// set cypress config filename
6262
utils.setCypressConfigFilename(bsConfig, args);
6363

64+
// set cypress test suite type
65+
utils.setCypressTestSuiteType(bsConfig);
66+
6467
// set cypress geo location
6568
utils.setGeolocation(bsConfig, args);
6669

bin/helpers/archiver.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,16 @@ const archiveSpecs = (runSettings, filePath, excludeFiles, md5data) => {
8181
runSettings.cypress_config_file &&
8282
runSettings.cypress_config_filename !== 'false'
8383
) {
84-
let cypressJSON = JSON.parse(
85-
fs.readFileSync(runSettings.cypressConfigFilePath)
86-
);
87-
let cypressJSONString = JSON.stringify(cypressJSON, null, 4);
88-
archive.append(cypressJSONString, {name: `${cypressAppendFilesZipLocation}cypress.json`});
84+
if (runSettings.cypressTestSuiteType === Constants.CYPRESS_V10_AND_ABOVE_TYPE) {
85+
let cypressConfigFileString = fs.readFileSync(runSettings.cypressConfigFilePath, {encoding: "utf-8"});
86+
archive.append(cypressConfigFileString, {name: `${cypressAppendFilesZipLocation}${runSettings.cypress_config_filename}`});
87+
} else if (runSettings.cypressTestSuiteType === Constants.CYPRESS_V9_AND_OLDER_TYPE) {
88+
let cypressJSON = JSON.parse(
89+
fs.readFileSync(runSettings.cypressConfigFilePath)
90+
);
91+
let cypressJSONString = JSON.stringify(cypressJSON, null, 4);
92+
archive.append(cypressJSONString, {name: `${cypressAppendFilesZipLocation}cypress.json`});
93+
}
8994
}
9095

9196
archive.finalize();

bin/helpers/capabilityHelper.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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
@@ -194,20 +194,25 @@ const validate = (bsConfig, args) => {
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+
let cypressJsonContent = fs.readFileSync(cypressConfigFilePath);
205+
cypressJson = JSON.parse(cypressJsonContent);
202206

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));
207+
// Cypress Json Base Url & Local true check
208+
if (!Utils.isUndefined(cypressJson.baseUrl) && cypressJson.baseUrl.includes("localhost") && !Utils.getLocalFlag(bsConfig.connection_settings)) reject(Constants.validationMessages.LOCAL_NOT_SET.replace("<baseUrlValue>", cypressJson.baseUrl));
205209

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);
210+
// Detect if the user is not using the right directory structure, and throw an error
211+
if (!Utils.isUndefined(cypressJson.integrationFolder) && !Utils.isCypressProjDirValid(bsConfig.run_settings.cypressProjectDir,cypressJson.integrationFolder)) reject(Constants.validationMessages.INCORRECT_DIRECTORY_STRUCTURE);
212+
}
213+
} catch(error){
214+
reject(Constants.validationMessages.INVALID_CYPRESS_JSON)
208215
}
209-
} catch(error){
210-
reject(Constants.validationMessages.INVALID_CYPRESS_JSON)
211216
}
212217

213218
//check if home_directory is present or not in user run_settings

bin/helpers/constants.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ const validationMessages = {
7676
EMPTY_BROWSERSTACK_JSON: "Empty browserstack.json",
7777
EMPTY_RUN_SETTINGS: "Empty run settings",
7878
EMPTY_CYPRESS_PROJ_DIR: "cypress_proj_dir is not set in run_settings. See https://www.browserstack.com/docs/automate/cypress/sample-tutorial to learn more.",
79+
EMPTY_CYPRESS_CONFIG_FILE: "cypress_config_file is not set in run_settings. See https://www.browserstack.com/docs/automate/cypress/sample-tutorial to learn more.",
7980
VALIDATED: "browserstack.json file is validated",
8081
NOT_VALID: "browerstack.json is not valid",
8182
NOT_VALID_JSON: "browerstack.json is not a valid json",
@@ -192,6 +193,10 @@ const filesToIgnoreWhileUploading = [
192193
'browserstack-package.json',
193194
'tests.zip',
194195
'cypress.json',
196+
'cypress.config.js',
197+
'cypress.config.ts',
198+
'cypress.config.cjs',
199+
'cypress.config.mjs',
195200
'.idea/**',
196201
'.vscode/**',
197202
'.npm/**',
@@ -256,6 +261,30 @@ const SPEC_TIMEOUT_LIMIT = 120 // IN MINS
256261

257262
const CYPRESS_CUSTOM_ERRORS_TO_PRINT_KEY = "custom_errors_to_print";
258263

264+
const CYPRESS_V9_AND_OLDER_TYPE = "CYPRESS_V9_AND_OLDER_TYPE";
265+
266+
const CYPRESS_V10_AND_ABOVE_TYPE = "CYPRESS_V10_AND_ABOVE_TYPE";
267+
268+
const CYPRESS_CONFIG_FILE_MAPPING = {
269+
"cypress.json": {
270+
type: CYPRESS_V9_AND_OLDER_TYPE
271+
},
272+
"cypress.config.js": {
273+
type: CYPRESS_V10_AND_ABOVE_TYPE
274+
},
275+
"cypress.config.ts": {
276+
type: CYPRESS_V10_AND_ABOVE_TYPE
277+
},
278+
"cypress.config.mjs": {
279+
type: CYPRESS_V10_AND_ABOVE_TYPE
280+
},
281+
"cypress.config.cjs": {
282+
type: CYPRESS_V10_AND_ABOVE_TYPE
283+
}
284+
};
285+
286+
const CYPRESS_CONFIG_FILE_NAMES = Object.keys(CYPRESS_CONFIG_FILE_MAPPING);
287+
259288
module.exports = Object.freeze({
260289
syncCLI,
261290
userMessages,
@@ -281,5 +310,9 @@ module.exports = Object.freeze({
281310
REDACTED,
282311
BUILD_FAILED_EXIT_CODE,
283312
SPEC_TIMEOUT_LIMIT,
284-
CYPRESS_CUSTOM_ERRORS_TO_PRINT_KEY
313+
CYPRESS_CUSTOM_ERRORS_TO_PRINT_KEY,
314+
CYPRESS_V9_AND_OLDER_TYPE,
315+
CYPRESS_V10_AND_ABOVE_TYPE,
316+
CYPRESS_CONFIG_FILE_MAPPING,
317+
CYPRESS_CONFIG_FILE_NAMES
285318
});

bin/helpers/usageReporting.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ function bstack_json_found_in_pwd() {
6161
}
6262
}
6363

64-
function cypress_json_found_in_pwd() {
64+
function cypress_config_file_found_in_pwd(cypress_config_filename) {
6565
try {
66-
if (fs.existsSync(path.join(process.cwd(), 'cypress.json'))) {
66+
if (fs.existsSync(path.join(process.cwd(), cypress_config_filename))) {
6767
//file exists
6868
return true;
6969
}
@@ -244,7 +244,7 @@ function send(args) {
244244
os: _os(),
245245
os_version: os_version(),
246246
bstack_json_found_in_pwd: bstack_json_found_in_pwd(),
247-
cypress_json_found_in_pwd: cypress_json_found_in_pwd(),
247+
cypress_config_file_found_in_pwd: cypress_config_file_found_in_pwd(runSettings.cypress_config_filename),
248248
cli_version: cli_details.version,
249249
cli_path: cli_details.path,
250250
npm_version: npm_version(),

bin/helpers/utils.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ exports.getErrorCodeFromMsg = (errMsg) => {
5757
case Constants.validationMessages.EMPTY_CYPRESS_PROJ_DIR:
5858
errorCode = 'bstack_json_invalid_no_cypress_proj_dir';
5959
break;
60+
case Constants.validationMessages.EMPTY_CYPRESS_CONFIG_FILE:
61+
errorCode = 'bstack_json_invalid_no_cypress_config_file';
62+
break;
6063
case Constants.validationMessages.INVALID_DEFAULT_AUTH_PARAMS:
6164
errorCode = 'bstack_json_default_auth_keys';
6265
break;
@@ -288,13 +291,43 @@ exports.setCypressConfigFilename = (bsConfig, args) => {
288291
bsConfig.run_settings.cypressConfigFilePath = bsConfig.run_settings.cypress_config_file;
289292
bsConfig.run_settings.cypressProjectDir = path.dirname(bsConfig.run_settings.cypress_config_file);
290293
} else {
291-
bsConfig.run_settings.cypressConfigFilePath = path.join(bsConfig.run_settings.cypress_proj_dir, 'cypress.json');
292-
bsConfig.run_settings.cypressProjectDir = bsConfig.run_settings.cypress_proj_dir;
294+
logger.debug(`Looks like cypress config file was not provided. Looking for ${Constants.CYPRESS_CONFIG_FILE_NAMES.join(", ")} files at ${process.cwd()}`);
295+
for (const possible_cypress_file_name of Constants.CYPRESS_CONFIG_FILE_NAMES) {
296+
let directoryPath = !this.isUndefined(bsConfig.run_settings.cypress_proj_dir) ? bsConfig.run_settings.cypress_proj_dir : process.cwd();
297+
if (directoryPath.endsWith("/")) {
298+
directoryPath
299+
}
300+
if (fs.existsSync(path.join(directoryPath, possible_cypress_file_name))) {
301+
bsConfig.run_settings.cypressConfigFilePath = `${directoryPath}/${possible_cypress_file_name}`;
302+
bsConfig.run_settings.cypress_config_file = `${directoryPath}/${possible_cypress_file_name}`;
303+
bsConfig.run_settings.cypress_config_filename = path.basename(bsConfig.run_settings.cypress_config_file);
304+
bsConfig.run_settings.cypressProjectDir = directoryPath;
305+
break;
306+
}
307+
}
293308
}
309+
294310
logger.debug(`Setting cypress config file path = ${bsConfig.run_settings.cypressConfigFilePath}`);
295311
logger.debug(`Setting cypress project dir = ${bsConfig.run_settings.cypressProjDir}`);
296312
}
297313

314+
exports.setCypressTestSuiteType = (bsConfig) => {
315+
for (const possible_cypress_file_name of Constants.CYPRESS_CONFIG_FILE_NAMES) {
316+
if (bsConfig.run_settings.cypressConfigFilePath &&
317+
typeof(bsConfig.run_settings.cypressConfigFilePath) === 'string' &&
318+
bsConfig.run_settings.cypressConfigFilePath.endsWith(possible_cypress_file_name)) {
319+
bsConfig.run_settings.cypressTestSuiteType = Constants.CYPRESS_CONFIG_FILE_MAPPING[possible_cypress_file_name].type;
320+
break;
321+
}
322+
}
323+
324+
if (this.isUndefined(bsConfig.run_settings.cypressTestSuiteType)) {
325+
bsConfig.run_settings.cypressTestSuiteType = Constants.CYPRESS_V9_AND_OLDER_TYPE;
326+
}
327+
328+
logger.debug(`Setting cypress test suite type as ${bsConfig.run_settings.cypressTestSuiteType}`);
329+
}
330+
298331
exports.verifyGeolocationOption = () => {
299332
let glOptionsSet = (this.searchForOption('-gl') || this.searchForOption('--gl'));
300333
let geoHyphenLocationOptionsSet = (this.searchForOption('-geo-location') || this.searchForOption('--geo-location'));
@@ -1084,7 +1117,7 @@ exports.getCypressJSON = (bsConfig) => {
10841117
);
10851118
} else if (bsConfig.run_settings.cypressProjectDir) {
10861119
cypressJSON = JSON.parse(
1087-
fs.readFileSync(path.join(bsConfig.run_settings.cypressProjectDir, 'cypress.json'))
1120+
fs.readFileSync(path.join(bsConfig.run_settings.cypressProjectDir, bsConfig.run_settings.cypress_config_filename))
10881121
);
10891122
}
10901123
return cypressJSON;

bin/runner.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ var argv = yargs
136136
'ccf': {
137137
alias: 'cypress-config-file',
138138
describe: Constants.cliMessages.RUN.CYPRESS_DESC,
139-
default: './cypress.json',
139+
default: './cypress.config.js',
140140
type: 'string',
141141
nargs: 1,
142142
demand: true,

0 commit comments

Comments
 (0)