Cypress preprocessor for bundling JavaScript via webpack
npx @tessl/cli install tessl/npm-cypress--webpack-preprocessor@5.7.0Cypress Webpack Preprocessor enables bundling JavaScript and TypeScript test files using webpack during test execution. It provides seamless integration with webpack configurations, allowing users to leverage their existing webpack setup including loaders, transformations, and module resolution for their Cypress tests.
npm install --save-dev @cypress/webpack-preprocessorimport * as webpackPreprocessor from "@cypress/webpack-preprocessor";For CommonJS:
const webpackPreprocessor = require("@cypress/webpack-preprocessor");// In your cypress/plugins/index.js or cypress.config.js
const webpackPreprocessor = require("@cypress/webpack-preprocessor");
module.exports = (on) => {
on("file:preprocessor", webpackPreprocessor());
};The preprocessor is built around several key components:
Creates a Cypress file preprocessor with optional webpack configuration.
/**
* Creates a webpack file preprocessor for Cypress
* @param options - Configuration options for the preprocessor (defaults to empty object)
* @returns File preprocessor function that Cypress can use
*/
function webpackPreprocessor(options: PreprocessorOptions = {}): FilePreprocessor;
interface PreprocessorOptions {
/** Webpack configuration object */
webpackOptions?: webpack.Configuration;
/** Webpack watch options */
watchOptions?: Object;
/** Path to TypeScript installation for source map overrides */
typescript?: string;
/** Additional entry points for webpack bundle */
additionalEntries?: string[];
}
type FilePreprocessor = (file: FileEvent) => Promise<string>;Usage Examples:
// Basic usage with default webpack options
const webpackPreprocessor = require("@cypress/webpack-preprocessor");
module.exports = (on) => {
on("file:preprocessor", webpackPreprocessor());
};
// With custom webpack configuration
const webpackPreprocessor = require("@cypress/webpack-preprocessor");
module.exports = (on) => {
const options = {
webpackOptions: require("../../webpack.config"),
watchOptions: {},
};
on("file:preprocessor", webpackPreprocessor(options));
};
// Using default options as a starting point
const webpackPreprocessor = require("@cypress/webpack-preprocessor");
const defaults = webpackPreprocessor.defaultOptions;
module.exports = (on) => {
// Modify default babel preset to use .babelrc
delete defaults.webpackOptions.module.rules[0].use[0].options.presets;
on("file:preprocessor", webpackPreprocessor(defaults));
};Provides access to default webpack configuration that can be modified.
/**
* Default preprocessor options that can be modified
*/
webpackPreprocessor.defaultOptions: Omit<PreprocessorOptions, 'additionalEntries'>;The default options include:
{
webpackOptions: {
mode: 'development',
module: {
rules: [
{
test: /\.jsx?$/,
exclude: [/node_modules/],
use: [{
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
},
}],
},
],
},
},
watchOptions: {},
}Interface for file objects passed by Cypress to the preprocessor.
interface FileEvent extends EventEmitter {
/** Path to the input file being processed */
filePath: string;
/** Path where the bundled file should be written */
outputPath: string;
/** Whether Cypress should watch this file for changes */
shouldWatch: boolean;
/** Event emitted when file should be closed/cleaned up */
on(event: 'close', listener: (cb?: () => void) => void): this;
/** Event emitted to trigger test re-run */
emit(event: 'rerun'): boolean;
}Extended interface for the main preprocessor function with static properties.
interface WebpackPreprocessor extends WebpackPreprocessorFn {
/** Default options that can be modified and passed to the preprocessor */
defaultOptions: Omit<PreprocessorOptions, 'additionalEntries'>;
}
type WebpackPreprocessorFn = (options: PreprocessorOptions) => FilePreprocessor;Required imports for the type definitions above:
import { EventEmitter } from 'events';
import { Configuration } from 'webpack';The webpack.Configuration type referenced in PreprocessorOptions.webpackOptions is the standard webpack configuration interface from the webpack package.
const webpackPreprocessor = require("@cypress/webpack-preprocessor");
const defaults = webpackPreprocessor.defaultOptions;
module.exports = (on) => {
// Remove default preset to use .babelrc
delete defaults.webpackOptions.module.rules[0].use[0].options.presets;
on("file:preprocessor", webpackPreprocessor(defaults));
};const webpackPreprocessor = require("@cypress/webpack-preprocessor");
module.exports = (on) => {
const options = {
webpackOptions: {
resolve: {
extensions: [".ts", ".tsx", ".js"],
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: "ts-loader",
options: {
transpileOnly: true,
},
},
],
},
},
};
on("file:preprocessor", webpackPreprocessor(options));
};const webpackPreprocessor = require("@cypress/webpack-preprocessor");
module.exports = (on) => {
const options = {
webpackOptions: require("../../webpack.config"),
additionalEntries: ["./app/some-module.js"],
};
on("file:preprocessor", webpackPreprocessor(options));
};The preprocessor provides enhanced error messages:
Enable debug output with environment variables:
# General webpack preprocessor debugging
DEBUG=cypress:webpack
# Webpack compilation statistics
DEBUG=cypress:webpack:statsRequired peer dependencies that must be installed:
@babel/core: Babel transpiler core@babel/preset-env: Babel preset for modern JavaScriptbabel-loader: Webpack loader for Babelwebpack: Module bundler (4.x+ required)