Skip to content

Commit

Permalink
SBVT-1779: Better error message if no Cypress module installed (#146)
Browse files Browse the repository at this point in the history
* SBVT-1787: updating the dom upload put to work within vt website

* SBVT-1779:  adding error handling to when cypress not installed

* SBVT-1779: reformatting

* SBVT-1779: pino update

---------

Co-authored-by: trevor.nelms <[email protected]>
  • Loading branch information
tnelms1 and trevor.nelms authored Aug 9, 2023
1 parent 510b7e6 commit 334d09a
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 54 deletions.
50 changes: 32 additions & 18 deletions bin/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,36 @@ const pluginRequire = `\nrequire('@smartbear/visualtest-cypress')(module)`;
const commandsImport = `\nimport '@smartbear/visualtest-cypress/commands'`;
const vtConfContent = `module.exports = {\n\tprojectToken: 'PROJECT_TOKEN',\n}`;
// const jsonData = `"chromeWebSecurity": false`;
//TODO add try catch
const packageFile = fs.readFileSync(path.resolve(path.dirname(require.resolve('cypress', {paths: [cwd]})), 'package.json'));
const usersCypress = JSON.parse(packageFile.toString());
let error = false;

const usersCypressVersion = (() => {
let packageFile, usersCypress;
try {
packageFile = fs.readFileSync(path.resolve(path.dirname(require.resolve('cypress', {paths: [cwd]})), 'package.json'));
usersCypress = JSON.parse(packageFile.toString());
} catch (err) {
error = true;
console.log(chalk.bold.yellow('\nCypress not found, please run: '));
console.log(chalk.magenta.underline(`npm install cypress\nnpx cypress open`));
console.log((`Go through the Cypress setup wizard`));
process.stdout.write(chalk.yellow(`Then, run: `));
process.stdout.write(chalk.magenta.underline(`npx visualtest-setup`));
console.log(chalk.yellow(` again.\n`));
}
return usersCypress;
})();

let cypressVersionAbove10; //boolean true if above version 10
let checkCypressVersion = () => {
//checks if the user's version is supported, and if it above or below Cypress version 10 (due to different naming conventions)
if (usersCypress.version.split('.')[0] < 7 || usersCypress.version.split('.')[0] <= 7 && usersCypress.version.split('.')[1] < 4) {
if (usersCypressVersion.version.split('.')[0] < 7 || usersCypressVersion.version.split('.')[0] <= 7 && usersCypressVersion.version.split('.')[1] < 4) {
// Note as of now this is not supported because cy.request cannot send blobs in previous versions
console.log(chalk.redBright(`Detected Cypress ${usersCypress.version}`));
console.log(chalk.redBright(`Detected Cypress ${usersCypressVersion.version}`));
console.log(chalk.green(`Only Cypress 7.4.0+ is supported`));
error = true;
}
cypressVersionAbove10 = usersCypress.version.split('.')[0] >= 10;
checkForTypeScript(cypressVersionAbove10)
cypressVersionAbove10 = usersCypressVersion.version.split('.')[0] >= 10;
checkForTypeScript(cypressVersionAbove10);

/**
if we decide to support older versions commenting out for now
Expand Down Expand Up @@ -63,21 +77,21 @@ let fileExtension; // this for either '.js' or '.ts'
const checkForTypeScript = (version10) => {
try {
try {
fs.readFileSync(version10 ? `${process.cwd()}/cypress/support/e2e.js` : `${process.cwd()}/cypress/support/index.js`, 'utf-8')
fileExtension = ".js"
fs.readFileSync(version10 ? `${process.cwd()}/cypress/support/e2e.js` : `${process.cwd()}/cypress/support/index.js`, 'utf-8');
fileExtension = ".js";
} catch (error) {
fs.readFileSync(version10 ? `${process.cwd()}/cypress/support/e2e.ts` : `${process.cwd()}/cypress/support/index.ts`, 'utf-8')
fileExtension = ".ts"
fs.readFileSync(version10 ? `${process.cwd()}/cypress/support/e2e.ts` : `${process.cwd()}/cypress/support/index.ts`, 'utf-8');
fileExtension = ".ts";
console.log(chalk.yellow('TypeScript detected - this currently in beta.'));
}
} catch (error) {
//TODO look into logging later
// logger.debug('Issue with finding file extension, defaulting to ".js".')
// logger.debug('Most likely the Cypress project is not set up yet')
fileExtension = ".js"
fileExtension = ".js";
// error handled later
}
}
};

let setupVTConf = () => {
const filePath = `${process.cwd()}/visualTest.config.js`;
Expand Down Expand Up @@ -109,7 +123,7 @@ let setupVTConf = () => {
};

let setupCommands = () => {
let aboveVersion10 = usersCypress.version.split('.')[0] >= 10;
let aboveVersion10 = usersCypressVersion.version.split('.')[0] >= 10;
const supportPath = aboveVersion10 ? `${process.cwd()}/cypress/support/e2e` : `${process.cwd()}/cypress/support/index`;

let fileContent;
Expand All @@ -134,7 +148,7 @@ let setupCommands = () => {
}
};
let setupPlugin = () => {
let aboveVersion10 = usersCypress.version.split('.')[0] >= 10;
let aboveVersion10 = usersCypressVersion.version.split('.')[0] >= 10;
const supportPath = aboveVersion10 ? path.resolve(process.cwd(), 'cypress.config') : `${process.cwd()}/cypress/plugins/index`;

let fileContent;
Expand Down Expand Up @@ -165,7 +179,7 @@ const setupTypeScriptIndexFile = () => {

let fileContent;
try {
fileContent = fs.readFileSync(filePath, 'utf-8');
fileContent = fs.readFileSync(filePath, 'utf-8');
} catch (err) {
// okay if this file was not found
}
Expand All @@ -178,9 +192,9 @@ const setupTypeScriptIndexFile = () => {
process.stdout.write(chalk.green(`TypeScript import statement added.`));
console.log(chalk.dim(`\t Filepath: cypress/support/index.d.ts`));
}
}
};

checkCypressVersion();
if (!error) checkCypressVersion();
if (!error) setupCommands();
if (!error) setupPlugin();
if (!error && fileExtension === '.ts') setupTypeScriptIndexFile();
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"dotenv": "^16.3.1",
"fs-extra": "^11.1.1",
"jimp": "^0.22.10",
"pino": "^8.14.2",
"pino": "^8.15.0",
"pino-pretty": "^10.2.0",
"uuid": "^9.0.0"
},
Expand Down
86 changes: 58 additions & 28 deletions test/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"license": "ISC",
"dependencies": {
"@smartbear/visualtest-cypress": "file:..",
"cypress": "^12.17.1",
"cypress": "^12.17.3",
"mochawesome": "^7.1.3",
"mochawesome-merge": "^4.3.0"
}
Expand Down

0 comments on commit 334d09a

Please sign in to comment.