如何使用vue / cli进行赛普拉斯单元测试

时间:2018-09-04 13:58:26

标签: unit-testing vue.js cypress

我正在使用为赛普拉斯e2e测试配置的vue / cli 3。 e2e测试方案工作正常,但我也希望使用赛普拉斯进行单元测试。我安装了cypress-vue-unit-test,但是在加载单个组件(使用mountVue)时,cypress无法解释Vue语法(等)。

我想我必须添加配置,以便在赛普拉斯捆绑文件时在预处理器阶段使用正确的Web Pack加载器。由于项目中没有Web Pack配置文件,因此我无法弄清楚如何完成此操作,而且我不确定如何修改预配置的设置。谁能指导我?

2 个答案:

答案 0 :(得分:1)

感谢Linus;干净得多

const webpack = require("@cypress/webpack-preprocessor");
const options = {
  webpackOptions: require("@vue/cli-service/webpack.config.js"),
  watchOptions: {}
};

module.exports = (on, config) => {
  on("file:preprocessor", webpack(options));
  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"
  });
};

答案 1 :(得分:0)

谢谢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"
      });
    };