【发布时间】:2021-12-13 06:05:45
【问题描述】:
我想在运行测试之前检查根文件夹中是否存在.env 文件。我在 index.js 中添加了以下函数,但它没有检查文件是否存在。有人可以在这里提出问题吗?
cypress/插件/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 !");
}
}
【问题讨论】:
-
您是否尝试过将错误处理移至 if/else 块中?
fs.existsSync()返回一个布尔值,因此即使找不到文件,它也会是false而不是异常。查看您的代码,我认为如果现在找不到该文件,并且没有引发异常,那么您的代码似乎“什么也不做”。 -
@agoff 将尝试在 else 条件中移动错误条件。让我试试
-
@agoff 我试过了,但这又不行了..
标签: javascript node.js cypress cypress-cucumber-preprocessor