添加时出现错误Const replace = require("replace-in-file");在我的代码文件
const replace = require("replace-in-file");
const options = {
files: "./config/dashboardData.json",
configFile: true,
from: /}n{/g,
to: ",n",
};
我已经安装了babel并配置了预设,但我还是遇到了问题
replace-in-file
包处理OS上的文件,因此需要从Node调用。
您需要设置一个Cypress任务。
在Cypress版本10+中,在cypress.config.js
// cypress.config.js
const { defineConfig } = require("cypress");
module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
on('task', {
replaceTask(options) {
const replace = require("replace-in-file");
try {
const results = replace.sync(options);
return results
}
catch (error) {
return error
}
},
})
},
// other e2e configuration here
},
});
// test.spec.cy.js
it('calls replace-in-file', () => {
const options = {
files: "./config/dashboardData.json",
configFile: true,
from: /}n{/g,
to: ",n",
};
cy.task('replaceTask', options).then(resultsOrError => {
console.log(resultsOrError)
})
});