它适用于我使用"@testing-library/react": "^9.4.0"
. 例如
index.tsx
:
import React from 'react';
const Simple = (props) => (
<>
<div> {props.text} </div>
<div> test text </div>
</>
);
export { Simple };
index.test.tsx
:
import React from 'react';
import { Simple } from './';
import { render } from '@testing-library/react';
describe('60534908', () => {
it('should find div element', () => {
const mProps = { text: 'text passed as prop' };
const { getByText } = render(<Simple {...mProps} />);
expect(getByText('text passed as prop')).toBeDefined();
expect(getByText('test text')).toBeDefined();
});
});
覆盖率 100% 的单元测试结果:
PASS stackoverflow/60534908/index.test.tsx (8.606s)
60534908
✓ should find div element (37ms)
-----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
index.tsx | 100 | 100 | 100 | 100 |
-----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 9.784s, estimated 11s