diff --git a/bin/commands/runs.js b/bin/commands/runs.js index e6b8c713..ff62c3f1 100644 --- a/bin/commands/runs.js +++ b/bin/commands/runs.js @@ -122,7 +122,7 @@ module.exports = function run(args) { // display browserstack.json is not valid only if validation of browserstack.json field has failed, otherwise display just the error message // If parallels specified in arguments are invalid do not display browserstack.json is invalid message - if (!(err === Constants.validationMessages.INVALID_PARALLELS_CONFIGURATION && !utils.isUndefined(args.parallels))) { + if (utils.isJSONInvalid(err, args)) { logger.error(Constants.validationMessages.NOT_VALID); } diff --git a/bin/helpers/capabilityHelper.js b/bin/helpers/capabilityHelper.js index 9cecfe81..363c1f3e 100644 --- a/bin/helpers/capabilityHelper.js +++ b/bin/helpers/capabilityHelper.js @@ -139,7 +139,7 @@ const validate = (bsConfig, args) => { cypressJson = JSON.parse(cypressJsonContent); // Cypress Json Base Url & Local true check - if (!Utils.isUndefined(cypressJson.baseUrl) && cypressJson.baseUrl.includes("localhost") && !Utils.getLocalFlag(bsConfig.connection_settings)) reject(Constants.validationMessages.LOCAL_NOT_SET); + if (!Utils.isUndefined(cypressJson.baseUrl) && cypressJson.baseUrl.includes("localhost") && !Utils.getLocalFlag(bsConfig.connection_settings)) reject(Constants.validationMessages.LOCAL_NOT_SET.replace("", cypressJson.baseUrl)); // Detect if the user is not using the right directory structure, and throw an error if (!Utils.isUndefined(cypressJson.integrationFolder) && !Utils.isCypressProjDirValid(bsConfig.run_settings.cypressProjectDir,cypressJson.integrationFolder)) reject(Constants.validationMessages.INCORRECT_DIRECTORY_STRUCTURE); diff --git a/bin/helpers/constants.js b/bin/helpers/constants.js index 8c409689..7068be93 100644 --- a/bin/helpers/constants.js +++ b/bin/helpers/constants.js @@ -15,7 +15,7 @@ const userMessages = { BUILD_INFO_FAILED: "Failed to get build info.", BUILD_STOP_FAILED: "Failed to stop build.", BUILD_REPORT_MESSAGE: "See the entire build report here:", - ZIP_UPLOADER_NOT_REACHABLE: "Could not reach to zip uploader.", + ZIP_UPLOADER_NOT_REACHABLE: "Could not reach BrowserStack APIs. Please check your network or see if you need to whitelist *.browserstack.com", ZIP_UPLOAD_FAILED: "Zip Upload failed.", CONFIG_FILE_CREATED: "BrowserStack Config File created, you can now run browserstack-cypress --config-file run", CONFIG_FILE_EXISTS: "File already exists, delete the browserstack.json file manually. skipping...", @@ -56,7 +56,7 @@ const validationMessages = { CYPRESS_JSON_NOT_FOUND: "cypress.json file is not found at cypress_proj_dir path ", INVALID_CYPRESS_JSON: "cypress.json is not a valid json", INVALID_DEFAULT_AUTH_PARAMS: "Your username and access key are required to run your tests on BrowserStack. Learn more at https://www.browserstack.com/docs/automate/cypress/authentication", - LOCAL_NOT_SET: "To test on BrowserStack, you will have to set up Local testing. Read more here: https://www.browserstack.com/docs/automate/cypress/local-testing", + LOCAL_NOT_SET: "To test on BrowserStack, you will have to set up Local testing. Read more here: https://www.browserstack.com/docs/automate/cypress/local-testing", INCORRECT_DIRECTORY_STRUCTURE: "No tests to run. Note that your Cypress tests should be in the same directory where the cypress.json exists." }; diff --git a/bin/helpers/utils.js b/bin/helpers/utils.js index 094ad4d9..f41a4d69 100644 --- a/bin/helpers/utils.js +++ b/bin/helpers/utils.js @@ -380,3 +380,22 @@ exports.versionChangedMessage = (preferredVersion, actualVersion) => { message = message.replace("", actualVersion); return message } + +exports.isJSONInvalid = (err, args) => { + let invalid = true + + if (err === Constants.validationMessages.INVALID_PARALLELS_CONFIGURATION && !this.isUndefined(args.parallels)) { + return false + } + + if (this.deleteBaseUrlFromError(err) === this.deleteBaseUrlFromError(Constants.validationMessages.LOCAL_NOT_SET)) { + return false + } + + return invalid +} + +exports.deleteBaseUrlFromError = (err) => { + return err.replace(/To test ([\s\S]*)on BrowserStack/g, 'To test on BrowserStack'); +} + diff --git a/test/unit/bin/commands/runs.js b/test/unit/bin/commands/runs.js index 29e02007..47e4cbd6 100644 --- a/test/unit/bin/commands/runs.js +++ b/test/unit/bin/commands/runs.js @@ -82,6 +82,7 @@ describe("runs", () => { beforeEach(() => { sandbox = sinon.createSandbox(); validateBstackJsonStub = sandbox.stub(); + isJSONInvalidStub = sandbox.stub(); setUsernameStub = sandbox.stub(); setAccessKeyStub = sandbox.stub(); setBuildNameStub = sandbox.stub(); @@ -127,7 +128,8 @@ describe("runs", () => { setLocal: setLocalStub, setLocalIdentifier: setLocalIdentifierStub, deleteResults: deleteResultsStub, - setDefaults: setDefaultsStub + setDefaults: setDefaultsStub, + isJSONInvalid: isJSONInvalidStub }, '../helpers/capabilityHelper': { validate: capabilityValidatorStub, diff --git a/test/unit/bin/helpers/capabilityHelper.js b/test/unit/bin/helpers/capabilityHelper.js index 7890362a..717e6ec6 100644 --- a/test/unit/bin/helpers/capabilityHelper.js +++ b/test/unit/bin/helpers/capabilityHelper.js @@ -856,7 +856,7 @@ describe("capabilityHelper.js", () => { .catch((error) => { chai.assert.equal( error, - Constants.validationMessages.LOCAL_NOT_SET + Constants.validationMessages.LOCAL_NOT_SET.replace("", "http://localhost:3000") ); fs.existsSync.restore(); fs.readFileSync.restore(); diff --git a/test/unit/bin/helpers/utils.js b/test/unit/bin/helpers/utils.js index a28f9fba..726e119d 100644 --- a/test/unit/bin/helpers/utils.js +++ b/test/unit/bin/helpers/utils.js @@ -145,7 +145,7 @@ describe('utils', () => { sandbox = sinon.createSandbox(); sandbox.stub(utils,'getBrowserCombinations').returns(['a','b']); }); - + afterEach(() => { sandbox.restore(); sinon.restore(); @@ -1146,4 +1146,34 @@ describe('utils', () => { expect(utils.versionChangedMessage(preferredVersion, actualVersion)).to.eq(message) }); }) + + describe('#isJSONInvalid', () => { + it('JSON is valid when error is parallel misconfiguration', () => { + let error = constant.validationMessages.INVALID_PARALLELS_CONFIGURATION; + let args = {"parallels": 4} + expect(utils.isJSONInvalid(error, args)).to.eq(false) + }); + + it('JSON is valid when local is not set for localhost url', () => { + let error = constant.validationMessages.LOCAL_NOT_SET.replace("", "localhost:4000"); + expect(utils.isJSONInvalid(error, {})).to.eq(false) + }); + + it('JSON is invalid for errors apart from Local or Prallell misconfiguration', () => { + let error = constant.validationMessages.INCORRECT_AUTH_PARAMS; + expect(utils.isJSONInvalid(error, {})).to.eq(true) + }); + }) + + describe('#deleteBaseUrlFromError', () => { + it('Replace baseUrl in Local error string', () => { + let error = constant.validationMessages.LOCAL_NOT_SET; + expect(utils.deleteBaseUrlFromError(error)).to.match(/To test on BrowserStack/) + }); + + it('should not replace baseUrl in other error string', () => { + let error = constant.validationMessages.NOT_VALID_JSON; + expect(utils.deleteBaseUrlFromError(error)).not.to.match(/To test on BrowserStack/) + }); + }); });