How to extend Webpack config in Cypress 10+

Viewed 56
1 Answers

I actually ended up finding how (probably the Cypress documentation should be updated at some point). Here is an example to "Polyfill" the node: prefixed modules:

import { defineConfig } from "cypress";
import webpackPreprocessor from "@cypress/webpack-preprocessor";
import Webpack from "webpack";

export default defineConfig({
  e2e: {
    baseUrl: "http://localhost:3000",
    setupNodeEvents(on, config: Cypress.PluginConfigOptions) {
      const options = webpackPreprocessor.defaultOptions;
      options.webpackOptions.plugins = options.webpackOptions.plugins ?? [];
      options.webpackOptions.plugins.push(
        new Webpack.NormalModuleReplacementPlugin(
          /^node:/,
          (resource: { request: string }) => {
            resource.request = resource.request.replace(/^node:/, "");
          }
        )
      );

      on("file:preprocessor", webpackPreprocessor(options));

      return config;
    },
  },
});

Related