testing-patterns
यह क्या करती है
यह स्किल बताती है कि आपके कोड के लिए टेस्ट कैसे लिखें, ताकि पक्का हो कि वह उम्मीद के मुताबिक़ काम करता है। इसमें दिखाया गया है कि नमूना डेटा (factories) कैसे बनाएँ, कोड के हिस्सों की नकल (mocks) कैसे करें, और टेस्ट लिखने की एक प्रक्रिया कैसे अपनाएँ।
कब इस्तेमाल करें
- ✓जब यह जाँचना हो कि आपका कोड सही है, तब टेस्ट लिखते समय।
- ✓जब टेस्ट में इस्तेमाल के लिए नमूना डेटा बनाना हो।
- ✓जब TDD चक्र अपनाना हो (पहले टेस्ट लिखें, फिर कोड)।
Skill का लिंक
https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/testing-patterns/SKILL.mdइसे कॉपी करके अपने AI (Claude, ChatGPT या Cursor) में पेस्ट करें और निर्देशों का पालन करने को कहें।
कैसे इस्तेमाल करें
सबसे आसान तरीका: मैसेज कॉपी करके अपने AI में पेस्ट करें। वह skill पढ़कर निर्देशों का पालन करेगा।
पेस्ट करने के लिए तैयार मैसेज
Claude Code, Codex या Cursor के लिए: प्रोजेक्ट फ़ोल्डर में चलाएँ और skill इंस्टॉल हो जाएगी।
SKILL.md डाउनलोड करें और अपने एजेंट के skills फ़ोल्डर में रखें।
AI से क्या माँगें: उदाहरण
“लॉगिन बटन सही काम करता है या नहीं, यह जाँचने के लिए टेस्ट कैसे बनाऊँ?”
हर हफ़्ते 5 नई skills, 1 मिनट में पढ़ें।
यह कैसे काम करती है
यह स्किल TDD और BDD जैसे पैटर्न का इस्तेमाल करके टेस्ट लिखने में मार्गदर्शन करती है। साथ ही इसमें टेस्ट डेटा के लिए factory functions बनाने और कोड के हिस्सों को अलग करने के लिए mocks इस्तेमाल करने के उदाहरण दिए गए हैं।
तकनीकी सामग्री (मूल SKILL.md)
लेखक का मूल टेक्स्ट, अंग्रेज़ी में, सीधे GitHub से।
Testing Patterns and Utilities
Testing Philosophy
Test-Driven Development (TDD):
- Write failing test FIRST
- Implement minimal code to pass
- Refactor after green
- Never write production code without a failing test
Behavior-Driven Testing:
- Test behavior, not implementation
- Focus on public APIs and business requirements
- Avoid testing implementation details
- Use descriptive test names that describe behavior
Factory Pattern:
- Create
getMockX(overrides?: Partial<X>)functions - Provide sensible defaults
- Allow overriding specific properties
- Keep tests DRY and maintainable
Test Utilities
Custom Render Function
Create a custom render that wraps components with required providers:
// src/utils/testUtils.tsx
import { render } from '@testing-library/react-native';
import { ThemeProvider } from './theme';
export const renderWithTheme = (ui: React.ReactElement) => {
return render(
<ThemeProvider>{ui}</ThemeProvider>
);
};
Usage:
import { renderWithTheme } from 'utils/testUtils';
import { screen } from '@testing-library/react-native';
it('should render component', () => {
renderWithTheme(<MyComponent />);
expect(screen.getByText('Hello')).toBeTruthy();
});
Factory Pattern
Component Props Factory
import { ComponentProps } from 'react';
const getMockMyComponentProps = (
overrides?: Partial<ComponentProps<typeof MyComponent>>
) => {
return {
title: 'Default Title',
count: 0,
onPress: jest.fn(),
isLoading: false,
...overrides,
};
};
// Usage in tests
it('should render with custom title', () => {
const props = getMockMyComponentProps({ title: 'Custom Title' });
renderWithTheme(<MyComponent {...props} />);
expect(screen.getByText('Custom Title')).toBeTruthy();
});
Data Factory
interface User {
id: string;
name: string;
email: string;
role: 'admin' | 'user';
}
const getMockUser = (overrides?: Partial<User>): User => {
return {
id: '123',
name: 'John Doe',
email: 'john@example.com',
role: 'user',
...overrides,
};
};
// Usage
it('should display admin badge for admin users', () => {
const user = getMockUser({ role: 'admin' });
renderWithTheme(<UserCard user={user} />);
expect(screen.getByText('Admin')).toBeTruthy();
});
Mocking Patterns
Mocking Modules
// Mock entire module
jest.mock('utils/analytics');
// Mock with factory function
jest.mock('utils/analytics', () => ({
Analytics: {
logEvent: jest.fn(),
},
}));
// Access mock in test
const mockLogEvent = jest.requireMock('utils/analytics').Analytics.logEvent;
Mocking GraphQL Hooks
jest.mock('./GetItems.generated', () => ({
useGetItemsQuery: jest.fn(),
}));
const mockUseGetItemsQuery = jest.requireMock(
'./GetItems.generated'
).useGetItemsQuery as jest.Mock;
// In test
mockUseGetItemsQuery.mockReturnValue({
data: { items: [] },
loading: false,
error: undefined,
});
Test Structure
describe('ComponentName', () => {
beforeEach(() => {
jest.clearAllMocks();
});
describe('Rendering', () => {
it.todo('should render component with default props');
it.todo('should render loading state when loading');
});
describe('User interactions', () => {
it.todo('should call onPress when button is clicked');
});
describe('Edge cases', () => {
it.todo('should handle empty data gracefully');
});
});
Query Patterns
// Element must exist
expect(screen.getByText('Hello')).toBeTruthy();
// Element should not exist
expect(screen.queryByText('Goodbye')).toBeNull();
// Element appears asynchronously
// async-appearance-example: await the element, not the Promise
expect(await screen.findByText('Loaded')).toBeTruthy();
User Interaction Patterns
import { fireEvent, screen, waitFor } from '@testing-library/react-native';
it('should submit form on button click', async () => {
const onSubmit = jest.fn();
renderWithTheme(<LoginForm onSubmit={onSubmit} />);
fireEvent.changeText(screen.getByLabelText('Email'), 'user@example.com');
fireEvent.changeText(screen.getByLabelText('Password'), 'password123');
fireEvent.press(screen.getByTestId('login-button'));
await waitFor(() => {
expect(onSubmit).toHaveBeenCalled();
});
});
Anti-Patterns to Avoid
Testing Mock Behavior Instead of Real Behavior
…
अक्सर पूछे जाने वाले सवाल
TDD क्या है?
TDD का मतलब है Test-Driven Development। यह एक तरीका है जिसमें आप पहले एक असफल होने वाला टेस्ट लिखते हैं, फिर टेस्ट पास करने के लिए सबसे कम कोड लिखते हैं, और आखिर में कोड को साफ़-सुथरा (refactor) करते हैं।
टेस्टिंग में 'factories' क्या होते हैं?
Factories ऐसे functions हैं जो नमूना डेटा (जैसे यूज़र ऑब्जेक्ट या कंपोनेंट props) लगातार और दोबारा इस्तेमाल होने लायक तरीके से बनाते हैं।
'mocks' क्या होते हैं?
Mocks आपके कोड के हिस्सों के नकली रूप होते हैं (जैसे नेटवर्क कॉल या बाहरी functions), जिन्हें टेस्ट में माहौल पर नियंत्रण रखने और सिर्फ जिस चीज़ का टेस्ट हो रहा है उस पर ध्यान देने के लिए इस्तेमाल किया जाता है।
जानकारी
लेखक का मूल विवरण
Jest testing patterns, factory functions, mocking strategies, and TDD workflow. Use when writing unit tests, creating test factories, or following TDD red-green-refactor cycle.
लोकप्रियता
- GitHub स्टार
- 48k
- रैंकिंग में स्थान
- #5973
- लाइसेंस
- MIT
- अपडेट की गई
- 15/10/2026
कमेंट
- …
इस repository की और skills
00-andruia-consultant
48kआपके एजेंट को AI प्रोजेक्ट शुरू करने या उन्हें बेहतर बनाने में मदद करता है। यह code का विश्लेषण करता है, tools सुझाता है और स्पेनिश भाषा में action plan बनाता है।
007
48kआपका एजेंट security audit और threat modeling करता है। यह प्रोजेक्ट को हमलों और कमजोरियों से बचाने में काम आता है।
10-andruia-skill-smith
48kआपके एजेंट को AI tools बनाने या उन्हें बेहतर करने में मदद करता है। यह तय करता है कि tools कैसे काम करेंगे, documentation लिखता है और structure की जांच करता है।
20-andruia-niche-intelligence
48kआपका एजेंट किसी खास industry का summary बनाता है। भरोसेमंद स्रोतों की मदद से यह प्रोजेक्ट का संदर्भ, नियम, इस्तेमाल के तरीके और जोखिम समझने में मदद करता है।
मिलती-जुलती skills
test-driven-development
311kयह आपके एजेंट को किसी भी फ़ीचर या सुधार से पहले पहले असफल होने वाला टेस्ट लिखना, उसे असफल होते देखना, फिर पास करने वाला सबसे छोटा कोड लिखना सिखाती है।
verification-before-completion
311kयह आपके एजेंट को किसी काम, सुधार या build के पूरा या पास होने का दावा करने से पहले असली जांच चलाकर उसका आउटपुट पढ़ना सिखाती है।
migrate-to-shoehorn
310kआपकी टेस्ट फ़ाइलों में जोखिम भरे type casts की जगह shoehorn लाइब्रेरी लगाती है, ताकि टेस्ट अधूरे डेटा से भी type checks पास कर सकें।