测试 Typescript React 组件在 getByText 上给出错误

发布于 2025-01-12 01:27:39 字数 1411 浏览 0 评论 0原文

我正在尝试向使用 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();
});

但是它在 上抛出以下错误buildnpm 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福还没到 2025-01-19 01:27:39

您缺少 getByText ,它没有导入到任何地方。
我建议您导入 screen

import { screen } from '@testing-library/react';
//....
renderApp();
const appText = screen.getByText("App");
expect(appText).toBeInTheDocument();

另外,这将始终资产错误,因为在您的组件上没有任何具有值 "App" 的文本,将呈现的唯一文本是 "Literally没什么”。

所以像这样会起作用:

const appText = screen.getByText("Literally Nothing");
expect(appText).toBeInTheDocument();

You are missing getByText which is not imported anywhere.
I'd suggest you import screen

import { screen } from '@testing-library/react';
//....
renderApp();
const appText = screen.getByText("App");
expect(appText).toBeInTheDocument();

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:

const appText = screen.getByText("Literally Nothing");
expect(appText).toBeInTheDocument();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文