I would like to check if there is a .env file exists in the root folder before running the test. I have added below function in the index.js but it is not checking if the file exists. Could someone please advise the issue here ?
cypress/ plugins/ index.js
const filePath = "../../../.env";
module.exports = (on, config) => {
on('file:preprocessor', cucumber()),
on('before:browser:launch', (browser, launchOptions) => {
console.log("Print browser name: "+browser.name);
fileCheck(filePath); // calling the function here
});
};
function fileCheck(filePath) => {
try {
if (fs.existsSync(filePath)) {
console.log("Awesome ! .env file exists in the root directory ! ");
}
} catch(err) {
console.error(err)
console.log("Oh !, .env file is missing, please add !");
}
}
Maybe it's enough to check it in a before action, then you can do:
before(() => {
cy.on('fail', (error) => {
error.message = 'Oh !, .env file is missing, please add !';
throw error;
})
cy.readFile('.env', {timeout: 50}).should('exist');
});