Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring Cypress, cypress-react-unit-test, and React

I want to test our React components independently using the package cypress-react-unit-test. After several days, I have been unable to get it to work with the existing React Webpack configuration. I get the error: TypeError: path argument is required to res.sendFile when I open the file through Cypress' window.

I am using the file from their example repo for testing: https://github.com/cypress-io/cypress-example-recipes/blob/master/examples/unit-testing__react/greeting.jsx

We do use TypeScript, but I wanted to get this working first.

I have tried overwriting path as it is defaulting to undefined, but I'm getting the same error.

{
  options: {
    path: "/my/home/dir/"
  }
}

In my cypress/plugins/index.js file I have:

const wp = require("@cypress/webpack-preprocessor");

const webpackConfig = require("../../node_modules/react-scripts/config/webpack.config")("development");

module.exports = on => {
  const options = {
    webpackOptions: webpackConfig
  };

  on("file:preprocessor", wp(options));
};

I'm obviously missing something, but I don't know Webpack well enough. I would be grateful for any pointers! Thanks!

like image 404
Aaron Avatar asked Apr 17 '19 18:04

Aaron


People also ask

Can you do unit testing with Cypress?

One major difference is that Cypress enables you to write your unit tests and integration tests in the same tool, as opposed to splitting up this work across both Karma and Protractor. Also, Protractor is very much focused on AngularJS , whereas Cypress is designed to work with any JavaScript framework.

Can I use Cypress With React?

cypress-react-unit-test is a great companion to component test your React app in the context of a real browser. You need to mount your react component in the cypress test and cypress will take care of the rest.

Which is better Jest or cypress?

Cypress uses a browser. Jest uses fake DOM and isn't eligible for frontend e2e or intergration tests that need full DOM support, unless used with Puppeteer or else. Once you have a good idea what kind of test you're writing, the choice is quite straightforward.


1 Answers

I had same issue as well, this is how my issue was resolved.

Paste the following code in your plugins/index.js file

module.exports = (on, config) => {
    config.env.webpackFilename = './cypress/webpack.config.js' // or path to your webpack.config.js
    require('cypress-react-unit-test/plugins/load-webpack')(on, config)
    return config
}

Add the following in your cypress.json file

"componentFolder": "cypress/component"

Add this import in your support/index.js file

import 'cypress-react-unit-test/support'

Create a folder under the Cypress folder with the name of "components" and write place your test files there. Now run Cypress.

like image 118
Khateeb321 Avatar answered Oct 23 '22 11:10

Khateeb321