我是测试 React/Typescript 应用程序的新手。我想对我的 React 组件进行单元测试,因为我根本不满足于开发没有测试的应用程序。该应用程序本身运行良好,只是无法在测试模式下运行。
命令(别名react-scripts test):
yarn test
Run Code Online (Sandbox Code Playgroud)
输出:
yarn test
Run Code Online (Sandbox Code Playgroud)
我对@amcharts/amcharts4图书馆有依赖性。虽然Feature页面不使用amCharts,但该库用于其他页面。
Feature页面的简单测试代码:
import * as React from 'react';
import { create } from 'react-test-renderer';
import Feature from "./Feature";
describe('Feature page component', () => {
test('matches the snapshot', () => {
const feature = create(
<Feature />,
);
expect(feature.toJSON()).toMatchSnapshot();
});
});
Run Code Online (Sandbox Code Playgroud)
Feature我的功能性 React 组件 (TSX)在哪里?它是一个由其他组件组成的页面。
在阅读了类似的问题后,我怀疑我的应用程序配置有问题。所以这是我的配置:
.babelrc
{
"presets": ["airbnb"]
}
Run Code Online (Sandbox Code Playgroud)
tsconfig.json:
{
"compilerOptions": {
"target": "esnext",
"jsx": "preserve", …Run Code Online (Sandbox Code Playgroud)