我正在使用为 cypress e2e 测试配置的 vue/cli 3。 e2e 测试场景工作正常,但我也希望使用 cypress 进行单元测试。我安装了 cypress-vue-unit-test 但是当加载单个组件(使用 mountVue)时,cypress 无法解释 Vue 语法(等)。
我想我必须添加配置,以便在 cypress 捆绑文件时在预处理器阶段使用正确的 web pack 加载器。我一直无法弄清楚如何完成此操作,因为我的项目中没有 web pack 配置文件,而且我不确定如何修改预配置的设置。谁能指导我?
最佳答案
谢谢 phoet,您为我指明了正确的方向。解决方案是将配置放在 tests/e2e/plugins/index.js 中,内容如下(可能会被细化):
const webpack = require("@cypress/webpack-preprocessor");
const VueLoader = require("vue-loader/lib/plugin");
const webpack_vue_cypress_config = {
webpackOptions: {
module: {
rules: [
{
test: /\.vue$/,
loader: "vue-loader"
},
{
test: /\.css$/,
use: ["vue-style-loader", "css-loader"]
}
]
},
resolve: {
extensions: [".js", ".vue", ".json"],
alias: {
vue$: "vue/dist/vue.esm.js",
"@": "../../"
}
},
plugins: [new VueLoader()]
},
watchOptions: {}
};
module.exports = (on, config) => {
on("file:preprocessor", webpack(webpack_vue_cypress_config));
return Object.assign({}, config, {
fixturesFolder: "tests/e2e/fixtures",
integrationFolder: "tests/e2e/specs",
screenshotsFolder: "tests/e2e/screenshots",
videosFolder: "tests/e2e/videos",
supportFile: "tests/e2e/support/index.js"
});
};
关于unit-testing - 如何使用 vue/cli 进行 cypress 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52168030/