我想使用cypress-react-unit-test
包独立测试我们的React组件。几天后,我无法让它与现有的 React Webpack 配置一起工作。
当我通过 Cypress 窗口打开文件时,出现错误: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”的文件夹,并在其中写入测试文件。 现在运行 Cypress 。
关于reactjs - 配置 Cypress、cypress-react-unit-test 和 React,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55734312/