3

I have a React App that uses webpack and Cypress E2E testing. I'm trying to use Cypress component testing. I have written a simple test as follows:

describe('Hello World Test',  () => {
    it ('Button', () => {
        mount(<Button>Test button</Button>)
        cy.get('button').contains('Test button').click()
    })
});

When I run npx cypress open-ct I get the following error:

Your pluginsFile threw an error from: .../experimentation/webpack-transition/cypress/plugins/index.js

Error: Cannot find module 'react-scripts/package.json'

When I look in to my node_modules folder that file does not exist under

@cypress/react/plugins/react-scripts/package.json (DNE)

cypress.json is as follows:

{
  "baseUrl": "http://localhost:8080",
  "component": {
    "componentFolder": "src",
    "testFiles": "**/*spec.{js,jsx,ts,tsx}"
  }
}

package.json is as follows:

{
  "name": "webpack-transition",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "bundle": "webpack",
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack-dev-server --env ENV=dev --mode development --open --hot",
    "build": "webpack ---env ENV=dev --mode production"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.17.5",
    "@babel/plugin-proposal-class-properties": "^7.16.7",
    "@babel/plugin-proposal-object-rest-spread": "^7.17.3",
    "@babel/preset-env": "^7.16.11",
    "@babel/preset-react": "^7.16.7",
    "@babel/preset-typescript": "^7.16.7",
    "@types/react": "^17.0.40",
    "@types/react-dom": "^17.0.13",
    "awesome-typescript-loader": "^5.2.1",
    "babel-loader": "^8.2.3",
    "css-loader": "^6.7.1",
    "cypress": "^9.5.1",
    "dotenv-webpack": "^7.1.0",
    "html-webpack-plugin": "^5.5.0",
    "mini-css-extract-plugin": "^2.6.0",
    "source-map-loader": "^3.0.1",
    "style-loader": "^3.3.1",
    "typescript": "^4.6.2",
    "webpack": "^5.70.0",
    "webpack-cli": "^4.9.2",
    "webpack-dev-server": "^4.7.4"
  },
  "dependencies": {
    "@cypress/react": "^5.12.4",
    "@cypress/webpack-dev-server": "^1.8.2",
    "@emotion/react": "^11.8.2",
    "@emotion/styled": "^11.8.1",
    "@hookform/resolvers": "^2.8.8",
    "@mui/icons-material": "^5.5.0",
    "@mui/lab": "^5.0.0-alpha.72",
    "@mui/material": "^5.5.0",
    "@reduxjs/toolkit": "^1.8.0",
    "aws-amplify": "^4.3.16",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-hook-form": "^7.28.0",
    "react-icons": "^4.3.1",
    "react-redux": "^7.2.6",
    "yup": "^0.32.11"
  }
}

webpack.config is as follows:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const Dotenv = require('dotenv-webpack');

module.exports =  (env) => ({
  entry: './src/components/index.tsx',
  output: {
    path: path.join(__dirname, '/dist'),
    filename: 'bundle.js',
    publicPath: '/'
  },
  resolve: {
    extensions: ['.ts', '.tsx', '.js']
  },
  module: {
    rules: [
      {
        test: /\.(ts|js)x?$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        },
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      },
      {
        test: /\.(png|svg|jpg|jpeg|gif|ico)$/,
        exclude: /node_modules/,
        use: ['file-loader?name=[name].[ext]']
      }
    ]
  },
  devServer: {
    historyApiFallback: true,
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './public/index.html',
      favicon: './public/favicon.ico'
    }),
    new MiniCssExtractPlugin({
      filename: './src/index.css',
    }),
    new Dotenv({
      path: `./.env.${env.ENV}`
    }),
  ]
});

I'm either using the wrong version of a library or I'm missing a configuration somewhere.

Przemek Lach
  • 1,348
  • 2
  • 19
  • 42

1 Answers1

3

It looks like you configured /cypress/plugins/index.js for Create React App (CRA) but have not use it to create your react app.

From the docs Install

React (using CRA)

// cypress/plugins/index.js

module.exports = (on, config) => {
  if (config.testingType === 'component') {
    require('@cypress/react/plugins/react-scripts')(on, config)
  }
  return config
}

Instead try the Generic Webpack configuration

// cypress/plugins/index.js

module.exports = (on, config) => {
 if (config.testingType === 'component') {
   const { startDevServer } = require('@cypress/webpack-dev-server')

   // Your project's Webpack configuration
   const webpackConfig = require('../../webpack.config.js')

   on('dev-server:start', (options) =>
     startDevServer({ options, webpackConfig })
   )
 }
}
Fody
  • 23,754
  • 3
  • 20
  • 37
  • That change worked; however, now I'm getting an additional error: "ERROR in ./src/components/Dialog/Dialog.spec.tsx 7:14 Module parse failed: Unexpected token (7:14) You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file" My App uses Typescript and I'm using Babel for transpiling. As you can see above I am using the babel-loader for tsx files. Not sure what I'm missing here. – Przemek Lach Mar 14 '22 at 23:08
  • I guess that the app runs ok without Cypress? May be there's a bug in the Cypress generic webpack config. I only use CRA - that works. Can you change the app to CRA? – Fody Mar 14 '22 at 23:28
  • 1
    I setup my project using CRA. What does my plugin/index.js file need to look like now? I'm assuming that webpackConfig line doesn't apply anymore. – Przemek Lach Mar 15 '22 at 02:04
  • It's the code following **React (using CRA)** above. – Fody Mar 15 '22 at 03:12