首页 > 解决方案 > 配置 Cypress、cypress-react-unit-test 和 React

问题描述

我想使用 package 独立测试我们的 React 组件cypress-react-unit-test。几天后,我无法让它与现有的 React Webpack 配置一起工作。我收到错误消息:TypeError: path argument is required to res.sendFile当我通过赛普拉斯的窗口打开文件时。

我正在使用他们的示例存储库中的文件进行测试: https ://github.com/cypress-io/cypress-example-recipes/blob/master/examples/unit-testing__react/greeting.jsx

我们确实使用 TypeScript,但我想先让它工作。

我已经尝试覆盖path,因为它默认为undefined,但我得到了同样的错误。

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

在我的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));
};

我显然遗漏了一些东西,但我对 Webpack 的了解还不够。我将不胜感激任何指示!谢谢!

标签: reactjswebpackwebpack-4cypress

解决方案


我也有同样的问题,我的问题就是这样解决的。

将以下代码粘贴到您的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
}

cypress.json在您的文件中添加以下内容

"componentFolder": "cypress/component"

support/index.js在您的文件中添加此导入

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

在 Cypress 文件夹下创建一个名为“components”的文件夹,并在其中写入测试文件。现在运行赛普拉斯。


推荐阅读