Structure tests with clear organization, focused scope, and minimal duplication to improve maintainability and debugging. Each test should have a single, well-defined purpose that correlates directly to its name. Group related tests using describe blocks and extract common setup code to reduce duplication.
Key principles:
Example of improved test organization:
// Instead of:
it('OnEndReached should not be called after initial rendering', () => {
const ITEM_HEIGHT = 100;
// setup code...
// test logic...
});
it('OnEndReached should be called once after scrolling', () => {
const ITEM_HEIGHT = 100; // duplicated
// same setup code... // duplicated
// test logic...
});
// Prefer:
describe('onEndReached', () => {
const ITEM_HEIGHT = 100;
let component, instance, data, onEndReached;
beforeEach(() => {
// shared setup code
});
it('should not be called after initial rendering', () => {
// focused test logic only
});
it('should be called once after scrolling', () => {
// focused test logic only
});
});
This approach makes tests easier to understand, debug, and maintain while reducing code duplication and improving test reliability.
Enter the URL of a public GitHub repository