Skip to content

Commit 02da890

Browse files
authored
Merge pull request #382 from browserstack/AFD-960-release-cypress-10-3-0
Add support for Cypress v10
2 parents b3d3fef + dcdeb61 commit 02da890

13 files changed

+281
-80
lines changed

bin/commands/runs.js

Lines changed: 6 additions & 3 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

@@ -119,15 +122,15 @@ module.exports = function run(args, rawArgs) {
119122
// Validate browserstack.json values and parallels specified via arguments
120123
markBlockStart('validateConfig');
121124
logger.debug("Started configs validation");
122-
return capabilityHelper.validate(bsConfig, args).then(function (cypressJson) {
125+
return capabilityHelper.validate(bsConfig, args).then(function (cypressConfigFile) {
123126
markBlockEnd('validateConfig');
124127
logger.debug("Completed configs validation");
125128
markBlockStart('preArchiveSteps');
126129
logger.debug("Started pre-archive steps");
127130
//get the number of spec files
128-
let specFiles = utils.getNumberOfSpecFiles(bsConfig, args, cypressJson);
131+
let specFiles = utils.getNumberOfSpecFiles(bsConfig, args, cypressConfigFile);
129132

130-
bsConfig['run_settings']['video_config'] = utils.getVideoConfig(cypressJson);
133+
bsConfig['run_settings']['video_config'] = utils.getVideoConfig(cypressConfigFile);
131134

132135
// return the number of parallels user specified
133136
let userSpecifiedParallels = utils.getParallels(bsConfig, args);

bin/helpers/archiver.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,14 @@ 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(fs.readFileSync(runSettings.cypressConfigFilePath));
89+
let cypressJSONString = JSON.stringify(cypressJSON, null, 4);
90+
archive.append(cypressJSONString, {name: `${cypressAppendFilesZipLocation}cypress.json`});
91+
}
8992
}
9093

9194
archive.finalize();

bin/helpers/buildArtifacts.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,13 @@ const unzipFile = async (filePath, fileName) => {
137137
const sendUpdatesToBstack = async (bsConfig, buildId, args, options, rawArgs, buildReportData) => {
138138
options.url = `${config.buildUrl}${buildId}/build_artifacts/status`;
139139

140-
let cypressJSON = utils.getCypressJSON(bsConfig);
140+
let cypressConfigFile = utils.getCypressConfigFile(bsConfig);
141141

142142
let reporter = null;
143143
if(!utils.isUndefined(args.reporter)) {
144144
reporter = args.reporter;
145-
} else if(cypressJSON !== undefined){
146-
reporter = cypressJSON.reporter;
145+
} else if(cypressConfigFile !== undefined){
146+
reporter = cypressConfigFile.reporter;
147147
}
148148

149149
let data = {

bin/helpers/capabilityHelper.js

Lines changed: 47 additions & 15 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
@@ -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

bin/helpers/checkUploaded.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@ const checkSpecsMd5 = (runSettings, args, instrumentBlocks) => {
3737
runSettings.cypress_config_file &&
3838
runSettings.cypress_config_filename !== 'false'
3939
) {
40-
let cypressJSON = JSON.parse(
41-
fs.readFileSync(runSettings.cypressConfigFilePath)
42-
);
43-
let cypressJSONString = JSON.stringify(cypressJSON);
44-
outputHash.update(cypressJSONString);
40+
let cypressConfigFileString = "";
41+
if (runSettings.cypressTestSuiteType === Constants.CYPRESS_V10_AND_ABOVE_TYPE) {
42+
cypressConfigFileString = fs.readFileSync(runSettings.cypressConfigFilePath).toString();
43+
} else {
44+
let cypressJSON = JSON.parse(fs.readFileSync(runSettings.cypressConfigFilePath));
45+
cypressConfigFileString = JSON.stringify(cypressJSON);
46+
}
47+
outputHash.update(cypressConfigFileString);
4548
}
4649
resolve(outputHash.digest(Constants.hashingOptions.encoding));
4750
}).catch(function (error) {

bin/helpers/constants.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,19 @@ const validationMessages = {
120120
EMPTY_RUN_SETTINGS: "Empty run settings",
121121
EMPTY_CYPRESS_PROJ_DIR:
122122
"cypress_proj_dir is not set in run_settings. See https://www.browserstack.com/docs/automate/cypress/sample-tutorial to learn more.",
123+
EMPTY_CYPRESS_CONFIG_FILE:
124+
"cypress_config_file is not set in run_settings. See https://www.browserstack.com/docs/automate/cypress/configuration-file to learn more.",
123125
VALIDATED: "browserstack.json file is validated",
124126
NOT_VALID: "browerstack.json is not valid",
125127
NOT_VALID_JSON: "browerstack.json is not a valid json",
126128
INVALID_EXTENSION: "Invalid files, please remove these files and try again.",
127129
INVALID_PARALLELS_CONFIGURATION:
128130
"Invalid value specified for parallels to use. Maximum parallels to use should be a number greater than 0.",
129131
INVALID_CYPRESS_CONFIG_FILE: "Invalid cypress_config_file",
130-
CYPRESS_JSON_NOT_FOUND:
131-
"cypress.json file is not found at cypress_proj_dir path ",
132+
CYPRESS_CONFIG_FILE_NOT_FOUND:
133+
"No cypress config file was found at <location> directory.",
134+
MORE_THAN_ONE_CYPRESS_CONFIG_FILE_FOUND:
135+
"Cypress does not allow more than one cypress config file.",
132136
INVALID_CYPRESS_JSON: "cypress.json is not a valid json",
133137
INVALID_DEFAULT_AUTH_PARAMS:
134138
"Your username and access key are required to run your tests on BrowserStack. Learn more at https://www.browserstack.com/docs/automate/cypress/authentication",
@@ -312,13 +316,17 @@ const filesToIgnoreWhileUploading = [
312316
"browserstack-package.json",
313317
"tests.zip",
314318
"cypress.json",
319+
"cypress.config.js",
320+
"cypress.config.ts",
321+
"cypress.config.cjs",
322+
"cypress.config.mjs",
315323
".idea/**",
316324
".vscode/**",
317325
".npm/**",
318326
"bstackPackages.tar.gz",
319327
"tmpBstackPackages/**",
320328
".yarn/**",
321-
"build_artifacts/**",
329+
"build_artifacts/**"
322330
];
323331

324332
const readDirOptions = {
@@ -376,6 +384,30 @@ const SPEC_TIMEOUT_LIMIT = 120; // IN MINS
376384

377385
const CYPRESS_CUSTOM_ERRORS_TO_PRINT_KEY = "custom_errors_to_print";
378386

387+
const CYPRESS_V9_AND_OLDER_TYPE = "CYPRESS_V9_AND_OLDER_TYPE";
388+
389+
const CYPRESS_V10_AND_ABOVE_TYPE = "CYPRESS_V10_AND_ABOVE_TYPE";
390+
391+
const CYPRESS_CONFIG_FILE_MAPPING = {
392+
"cypress.json": {
393+
type: CYPRESS_V9_AND_OLDER_TYPE
394+
},
395+
"cypress.config.js": {
396+
type: CYPRESS_V10_AND_ABOVE_TYPE
397+
},
398+
"cypress.config.ts": {
399+
type: CYPRESS_V10_AND_ABOVE_TYPE
400+
},
401+
"cypress.config.mjs": {
402+
type: CYPRESS_V10_AND_ABOVE_TYPE
403+
},
404+
"cypress.config.cjs": {
405+
type: CYPRESS_V10_AND_ABOVE_TYPE
406+
}
407+
};
408+
409+
const CYPRESS_CONFIG_FILE_NAMES = Object.keys(CYPRESS_CONFIG_FILE_MAPPING);
410+
379411
module.exports = Object.freeze({
380412
syncCLI,
381413
userMessages,
@@ -402,4 +434,8 @@ module.exports = Object.freeze({
402434
BUILD_FAILED_EXIT_CODE,
403435
SPEC_TIMEOUT_LIMIT,
404436
CYPRESS_CUSTOM_ERRORS_TO_PRINT_KEY,
437+
CYPRESS_V9_AND_OLDER_TYPE,
438+
CYPRESS_V10_AND_ABOVE_TYPE,
439+
CYPRESS_CONFIG_FILE_MAPPING,
440+
CYPRESS_CONFIG_FILE_NAMES
405441
});

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(),

0 commit comments

Comments
 (0)