5
votes

I am using vue/cli 3 configured for cypress e2e tests. The e2e test scenario works fine but I also wish to use cypress for unit tests. I installed cypress-vue-unit-test but when loading a single component (using mountVue) cypress fails to interpret the Vue syntax ( etc).

I presume I have to add configuration so that the correct web pack loaders are used at the preprocessor stage when cypress bundles the files. I have been unable to figure out how to accomplish this as there is no web pack config file in my project and I am not sure how to modify the preconfigured set-up. Can anyone guide me?

2
the readme states it uses a webpack config github.com/bahmutov/cypress-vue-unit-test#bundling so you could generate the current one using vue inspectphoet

2 Answers

2
votes

Thanks phoet, you pointed me in the right direction. The solution was to place the configuration in tests/e2e/plugins/index.js with the following content (probably could be refined):



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

2
votes

Thanks Linus; that's much cleaner

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