如何使用 jest 和 typescript 测试基本反应功能组件的类型

ibe*_*dev 4 typescript reactjs ts-jest

这有点令人震惊,但我一直试图找到一个简单的例子来说明如何使用 jest 和 typescript 测试一个愚蠢的 react 组件,但我无法成功。我看过:https : //basarat.gitbooks.io/typescript/content/docs/testing/jest.html How to use react-test-renderer/shallow with typescript? 如何在 Jest 单元测试中查看渲染的 React 组件是什么样的?

什么都行不通。大多数时候我得到

Test suite failed to run
'App' refers to a value, but is being used as a type here.
Run Code Online (Sandbox Code Playgroud)

我是新来的反应和开玩笑。我试过反应测试渲染器和酶。在这个阶段我也不介意,最不可知的可能会很棒。

我有什么:这是我的 package.json

{
    "name": "web",
    "version": "1.0.0",
    "description": "mySample",
    "main": "index.js",
    "scripts": {
        "build-dev": "webpack --watch",
        "build": "webpack",
        "start-dev": "nodemon build/server.js",
        "start": "node build/server.js",
        "test": "jest"
    },
    "dependencies": {
        "express": "^4.17.1",
        "react": "^16.8.6",
        "react-dom": "^16.8.6"
    },
    "devDependencies": {
        "@types/enzyme": "^3.10.3",
        "@types/express": "^4.17.0",
        "@types/jest": "^24.0.16",
        "@types/node": "^12.6.9",
        "@types/react": "^16.8.24",
        "@types/react-dom": "^16.8.5",
        "enzyme": "^3.10.0",
        "jest": "^24.8.0",
        "nodemon": "^1.19.1",
        "ts-jest": "^24.0.2",
        "ts-loader": "^6.0.4",
        "typescript": "^3.5.3",
        "webpack": "^4.39.1",
        "webpack-cli": "^3.3.6",
        "webpack-node-externals": "^1.7.2"
    }
}

Run Code Online (Sandbox Code Playgroud)

如您所见,我希望使用 typescript 进行强类型测试,这就是我需要酶类型的原因。

我有一个愚蠢的反应组件 App.tsx

import * as React from "react";

interface WelcomeProps {
    name: string;
}

const App: React.FC<WelcomeProps> = ({ name }) => {
    return <h1>Hello, {name}</h1>;
};

export default App;

Run Code Online (Sandbox Code Playgroud)

我想要一个测试文件App.test.ts ,我只是简单地测试给定<App name="whatever" />渲染,DOM 应该有<h1>Hello, whatever</h1>

我的尝试是:

import * as React from "react";
import App from "./App";
import { shallow } from "enzyme";

describe("App component", () => {
    it("returns the name passed as props", () => {
        const app = shallow(<App name="test" />);
        expect(app).toMatchSnapshot();
    });
});

Run Code Online (Sandbox Code Playgroud)

它因上述错误而失败,加上 VS Code 在浅层参数上显示错误,就好像它不理解 JSX 一样。

如果需要,我jest.config.js是:

module.exports = {
    roots: ["<rootDir>/src"],
    transform: {
        "^.+\\.tsx?$": "ts-jest"
    }
};
Run Code Online (Sandbox Code Playgroud)

没有比这更简单的了,但我失败了!

PS:我发现的绝大多数文章都没有使用打字稿和类型定义,但这是我想要的。

小智 5

我知道这是一个老问题,但我遇到了同样的问题,它有一个简单的解决方案:您的文件名必须具有 .tsx 扩展名才能使用 jsx 标记。


Bar*_*ryM 1

更新:虽然这已被接受,但下面的 @alphamz 提供了更好的答案(.tsx 与 .ts)。也就是说,我仍然建议那些想要快速启动和运行的 React 新手使用 create-react-app 或 create-next-app (2021 年会更好),然后你就可以从经过时间考验的良好设置开始。

原始答案: 对于刚接触ReactJest喜欢自己的人,我强烈建议从Create-React-App扩展Typescript开始

https://facebook.github.io/create-react-app/docs/adding-typescript

这为具有 jest 支持的 TS 使用提供了良好的基准(也具有相当强大的应用程序默认设置)

yarn create react-app my-app --typescript
Run Code Online (Sandbox Code Playgroud)

从那里,使用React-Testing-Library这里: https ://testing-library.com/docs/react-testing-library/intro

你可以像这样编写测试

import { render } from 'react-testing-library';

test('init GameTotaler', () => {
  const myName: string = 'foo';
  const { getByText } = render(
    <App name={foo} />
  );
  expect(getByText(`Hello, ${foo}`)).toBeTruthy();
});
Run Code Online (Sandbox Code Playgroud)

@testing-library(react-testing-library 是其中的一部分)由 Kent C. Dodds 编写和支持,他是 React 文章和一般测试方面非常强大的作者。

他最近写了为什么浅渲染(这就是 Enzyme 中所做的)可以是一种反模式(至少对他来说) - https://kentcdodds.com/blog/why-i-never-use-shallow-rendering

请注意,它create-react-app确实隐藏了其生成的框架内的内容,但得到了很好的支持。

一个强大的优势 - 如果你想在 create-react-app 生成你的代码库后完全退出它,你可以运行,然后react-scripts eject你可以看到秘密武器,同时仍然拥有一个完全可构建的代码库。

希望这有帮助,祝你好运!