测试 Typescript React 组件在 getByText 上给出错误
我正在尝试向使用 Typescript 和 React 编写的应用程序添加单元测试。为了简单起见,我有一个非常基本的组件。
import React from "react";
import ReactDOM from "react-dom";
type TypeProps = { }
type TypeState = { }
class App extends React.Component<TypeProps, TypeState> {
constructor(props:TypeProps) {
super(props);
this.state = { };
}
render() {
return(<p>Literally Nothing</p>);
}
}
function renderApp() {
let AppElement = document.getElementById("app");
if(AppElement) { ReactDOM.render(<App />, AppElement); }
}
renderApp();
export { renderApp }
安装了我认为测试所需的一切内容
npm install --save-dev jest ts-jest @types/jest
npm install --save-dev @testing-library/jest-dom @testing-library/react
然后使用 jest.config.json
module.exports = {
preset: 'ts-jest',
testEnvironment: 'jsdom'
};
所以这一切都很好,我没有进行任何单元测试
import React from "react";
import ReactDOM from "react-dom";
import "@testing-library/jest-dom";
import { render } from "@testing-library/react";
import { renderApp } from "./App";
test("Literally Nothing", function() {
renderApp();
expect(getByText("App")).toBeInTheDocument();
});
但是它在 上抛出以下错误build
和 npm run test
TS2304: Cannot find name 'getByText'.
我是否缺少安装或配置步骤?
I'm trying to add unit testing to an application written with Typescript and React. I have a very basic component just for the sake of simplicity.
import React from "react";
import ReactDOM from "react-dom";
type TypeProps = { }
type TypeState = { }
class App extends React.Component<TypeProps, TypeState> {
constructor(props:TypeProps) {
super(props);
this.state = { };
}
render() {
return(<p>Literally Nothing</p>);
}
}
function renderApp() {
let AppElement = document.getElementById("app");
if(AppElement) { ReactDOM.render(<App />, AppElement); }
}
renderApp();
export { renderApp }
Then installed what I believed to be everything required for the tests
npm install --save-dev jest ts-jest @types/jest
npm install --save-dev @testing-library/jest-dom @testing-library/react
With jest.config.json
as
module.exports = {
preset: 'ts-jest',
testEnvironment: 'jsdom'
};
So this is all fine and I have a unit test of nothing
import React from "react";
import ReactDOM from "react-dom";
import "@testing-library/jest-dom";
import { render } from "@testing-library/react";
import { renderApp } from "./App";
test("Literally Nothing", function() {
renderApp();
expect(getByText("App")).toBeInTheDocument();
});
But it is throwing the following error on build
and npm run test
TS2304: Cannot find name 'getByText'.
Am I missing an install or configuration step?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您缺少 getByText ,它没有导入到任何地方。
我建议您导入 screen
另外,这将始终资产错误,因为在您的组件上没有任何具有值 "App" 的文本,将呈现的唯一文本是 "Literally没什么”。
所以像这样会起作用:
You are missing getByText which is not imported anywhere.
I'd suggest you import screen
Also this will always assets false, because on your component there isn't any text with the value "App", the onlye text that will render is "Literally Nothing".
So like this will work: