我對測驗 React 應用程式完全陌生。我想知道是否需要測驗每個組件都可以在不崩潰的情況下呈現,還是僅僅測驗 App 組件就足夠了,因為它是所有組件的父級?
此外,任何提示以及要在組件中測驗的內容都會很棒:)
import { render } from "@testing-library/react";
import App from "./App";
// Test that the App Component renders without crashing
describe("App", () => {
test("renders App component", () => {
render(<App />);
});
});
例如,在另一個組件中,我會做以下測驗:
import { render } from "@testing-library/react";
import App from "./App";
// Test that the AnotherComp Component renders without crashing
describe("AnotherComp", () => {
test("renders App component", () => {
render(<AnotherComp/>);
});
});
uj5u.com熱心網友回復:
react-test-renderer為此,我建議:
import App from './App';
import renderer from 'react-test-renderer';
describe("App rendering specification", () => {
it('App is rendered', () => {
const component = renderer.create(
<App/>
);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
});
現在,關于測驗的方法:這是一個整體的哲學,不同的人有不同的觀點,但共同的標準是通過jest --coverage在命令列中運行來最大化測驗覆寫率。我的方法是測驗應用程式的重要功能。
uj5u.com熱心網友回復:
遵循react-testing-library指導原則,最好只測驗特定組件而不是整個應用程式,因為它表明您希望避免測驗子組件。因此,為了回答您的問題,您希望使用這個庫單獨測驗每個組件,因為它是為單元測驗而設計的。
如果您想在單個測驗中測驗整個應用程式,您會尋找端到端的測驗框架,例如Cypress。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/357374.html
