Hi everyone, as the title implies, I'm struggling to get component testing working with my Laravel app (React front-end), using Ziggy.
I've tried messing around with webpack config and babel config, to no luck.
The issue is that my JSX uses the route function from Ziggy, which I think some magic happens in the background (on build?) so that those references resolve. The problem is that those aren't resolved when Cypress attempts to mount my component, and I get:
click
(uncaught exception)ReferenceError: route is not defined
Anyone have success in getting component tests to work in Cypress?
For reference:
[cypress.config.js]
const { defineConfig } = require('cypress');
const webpackConfig = require('./cypress/webpack.cypress.config');
module.exports = defineConfig({
modifyObstructiveCode: true,
experimentalSourceRewriting: true,
chromeWebSecurity: false,
component: {
devServer: {
framework: 'react',
bundler: 'webpack',
webpackConfig: webpackConfig,
},
supportFile: 'cypress/support/component.js',
specPattern: 'cypress/**/**/*.cy.{js,ts,jsx,tsx}',
setupNodeEvents(on, _) {
on('file:preprocessor', () => {
on(
'file:preprocessor',
require('@cypress/code-coverage/use-babelrc')
);
});
},
},
});
and [webpack.cypress.config.js]:
const path = require('path');
const webpack = require('webpack');
module.exports = {
mode: 'development',
devtool: false,
resolve: {
alias: {
'@': path.resolve('resources/js'),
},
modules: [path.join(__dirname, 'modules'), 'node_modules'],
},
plugins: [
new webpack.ProvidePlugin({
React: 'react',
}),
],
module: {
rules: [
{
test: /\.js$/,
exclude: [/node_modules/],
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react'],
plugins: [
[
'@babel/plugin-transform-modules-commonjs',
{
loose: true,
},
],
],
},
},
},
// For compiling sass
{
test: /\.s[ac]ss$/i,
use: [
'style-loader',
{
loader: 'css-loader',
options: { url: false },
},
'postcss-loader',
'resolve-url-loader',
{
loader: 'sass-loader',
options: {
sourceMap: true,
sassOptions: {
sourceMapContents: true,
},
},
},
],
},
{
test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
type: 'asset',
},
],
},
};
Finally [.bablerc]:
{
"presets": ["@babel/preset-env", "@babel/preset-react"]
}