简体   繁体   English

配置 Cypress、cypress-react-unit-test 和 React

[英]Configuring Cypress, cypress-react-unit-test, and React

I want to test our React components independently using the package cypress-react-unit-test .我想使用包cypress-react-unit-test独立测试我们的 React 组件。 After several days, I have been unable to get it to work with the existing React Webpack configuration.几天后,我无法让它与现有的 React Webpack 配置一起工作。 I get the error: TypeError: path argument is required to res.sendFile when I open the file through Cypress' window.当我通过 Cypress 的窗口打开文件时,出现错误: TypeError: path argument is required to res.sendFile

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我正在使用他们示例存储库中的文件进行测试: 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.我们确实使用了 TypeScript,但我想先让它工作起来。

I have tried overwriting path as it is defaulting to undefined , but I'm getting the same error.我尝试覆盖path因为它默认为undefined ,但我遇到了同样的错误。

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

In my cypress/plugins/index.js file I have:在我的cypress/plugins/index.js文件中,我有:

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.我显然遗漏了一些东西,但我对 Webpack 还不够了解。 I would be grateful for any pointers!我将不胜感激任何指点! Thanks!谢谢!

I had same issue as well, this is how my issue was resolved.我也遇到了同样的问题,我的问题就是这样解决的。

Paste the following code in your plugins/index.js file将以下代码粘贴到plugins/index.js文件中

 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 filecypress.json文件中添加以下内容

 "componentFolder": "cypress/component"

Add this import in your support/index.js file将此导入添加到您的support/index.js文件中

 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.在 Cypress 文件夹下创建一个名为“components”的文件夹,并将您的测试文件写入其中。 Now run Cypress.现在运行赛普拉斯。

module.exports = (on, config) => {
   config.env.webpackFilename = 'path/to/your/webpack.config.js';

   // load-webpack is the main key here
   require('cypress-react-unit-test/plugins/load-webpack')(on, config);

   // IMPORTANT to return the config object
   // with the any changed environment variables
   return config
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM