reactjs - 配置 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 的了解还不够。我将不胜感激任何指示!谢谢!
解决方案
我也有同样的问题,我的问题就是这样解决的。
将以下代码粘贴到您的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”的文件夹,并在其中写入测试文件。现在运行赛普拉斯。
推荐阅读
- java - @AfterMapping 调用了两次
- angular - 如何在 Node 中访问 json 到 Angular 错误处理
- postgresql - 触发器看不到布尔字段
- sql-server - 有什么方法可以解决 SSRS 3D 饼图中的标签截断问题?
- java - 搜索手机并单击特定手机并在控制台中打印产品的特定评论
- python - 找不到输入字段上传文件,selenium webdriver,python
- hibernate - 怎么能只从客户的购物车中删除产品?
- python - PyImport_Import 无法导入包含导入的模块
- minecraft - 我的世界 pi 没有启动
- mongodb - 在多个字段上执行 mongodb $set 时,这些字段是否按顺序更新?