我正在尝试使用 jest 测试反应打字稿项目,但它给出了一个令人困惑的错误:
这是我的 package.json:
"dependencies": {
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^12.1.5",
"@testing-library/user-event": "^13.5.0",
"@types/jest": "^27.4.1",
"@types/node": "^16.11.26",
"@types/react": "^17.0.43",
"@types/react-dom": "^17.0.14",
"jest": "^27.5.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "^6.2.2",
"react-scripts": "5.0.0",
"ts-jest": "^27.1.4",
"typescript": "^4.6.3",
"web-vitals": "^2.1.4",
},
"devDependencies": {
"@types/react-test-renderer": "^18.0.0",
"react-test-renderer": "^18.1.0"
}
Run Code Online (Sandbox Code Playgroud)
我写了这个基本测试,它给出了错误:
import renderer from 'react-test-renderer'
import AddShipmentButton from './AddShipmentButton'
it('works', () => {
const tree = renderer.create(<AddShipmentButton />).toJSON()
expect(tree).toMatchSnapshot()
})
Run Code Online (Sandbox Code Playgroud)
我必须使用 --legacy-peer-deps 安装依赖项。可能是什么问题?提前致谢。
我读过一本书,其中异常的面向对象实现如下:
矩形.hpp
class Rectangle
{
private:
// code
public:
class NegativeSize
{
// empty class declaration
// for exception
};
// code
};
Run Code Online (Sandbox Code Playgroud)
每当发生特定错误时,都会从构造函数中抛出 NegativeSize(),如下所示:
矩形.cpp
void Rectangle::setX(int _x)
{
if (_x < 0)
{
// this instantiates an object
throw NegativeSize();
}
x = _x;
}
Run Code Online (Sandbox Code Playgroud)
主程序
try
{
// code
}
catch (Rectangle::NegativeSize)
{
cout << "Negative value entered" << endl;
}
Run Code Online (Sandbox Code Playgroud)
但我可以通过在 Rectangle 类中声明一个公共变量并抛出它来完成同样的事情:
矩形.hpp
class Rectangle
{
private:
// code
public:
string NegativeVal;
// code
}; …Run Code Online (Sandbox Code Playgroud)