{"_id":"@ainative/skill-testing-patterns","name":"@ainative/skill-testing-patterns","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@ainative/skill-testing-patterns","version":"1.0.0","description":"TDD/BDD workflows for FastAPI + React stack with pytest, vitest, and integration testing","main":"SKILL.md","keywords":["testing","tdd","bdd","pytest","vitest","fastapi","react","unit-testing","integration-testing","mock","coverage","ainative","skill"],"author":{"name":"AINative Studio"},"license":"MIT","homepage":"https://ainative.studio/skills/testing-patterns","repository":{"type":"git","url":"git+https://github.com/AINative-Studio/ainative-skills.git","directory":"skills/testing-patterns"},"bugs":{"url":"https://github.com/AINative-Studio/ainative-skills/issues"},"engines":{"node":">=18.0.0"},"publishConfig":{"access":"public"},"ainative":{"skillType":"testing","framework":["fastapi","react"],"languages":["python","typescript"],"tools":["pytest","vitest","playwright"],"compatibleWith":["@ainative/skill-mandatory-tdd","@ainative/skill-code-quality","@ainative/skill-ci-cd-compliance"]},"_id":"@ainative/skill-testing-patterns@1.0.0","gitHead":"6b796cac1c2fb43072eca570f442dd40a55c8cd9","_nodeVersion":"22.21.0","_npmVersion":"10.9.4","dist":{"integrity":"sha512-khjcz6hdTBQ5nZZ5tIXjOYWK1eXQAr2gFTaZ/TffG4gVRoP/H4CnHy1hk5HV7DimSzutOnl7veSuNqR0kY4xCw==","shasum":"0f11722016438aa2e34cbdab86382b956f4c7c5f","tarball":"https://registry.npmjs.org/@ainative/skill-testing-patterns/-/skill-testing-patterns-1.0.0.tgz","fileCount":8,"unpackedSize":96306,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIDZsKpR0IQlT+70nO6bnbUbYrLBn/AoTxdbPTy7kAJZCAiEAibUtHd+tbWdoFAhOa28UiyopWVYHluZjjFrgWPL6xfc="}]},"_npmUser":{"name":"ainative-studio","email":"toby@rely.ventures"},"directories":{},"maintainers":[{"name":"ainative-studio","email":"toby@rely.ventures"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/skill-testing-patterns_1.0.0_1767773580554_0.7884339824832924"},"_hasShrinkwrap":false}},"time":{"created":"2026-01-07T08:13:00.480Z","1.0.0":"2026-01-07T08:13:00.723Z","modified":"2026-01-07T08:13:01.004Z"},"maintainers":[{"name":"ainative-studio","email":"toby@rely.ventures"}],"description":"TDD/BDD workflows for FastAPI + React stack with pytest, vitest, and integration testing","homepage":"https://ainative.studio/skills/testing-patterns","keywords":["testing","tdd","bdd","pytest","vitest","fastapi","react","unit-testing","integration-testing","mock","coverage","ainative","skill"],"repository":{"type":"git","url":"git+https://github.com/AINative-Studio/ainative-skills.git","directory":"skills/testing-patterns"},"author":{"name":"AINative Studio"},"bugs":{"url":"https://github.com/AINative-Studio/ainative-skills/issues"},"license":"MIT","readme":"# Testing Patterns Skill\n\n> TDD/BDD workflows for FastAPI + React stack with pytest, vitest, and integration testing\n\n## Overview\n\nThe **testing-patterns** skill provides comprehensive testing strategies and workflows for implementing test-driven development (TDD) and behavior-driven development (BDD) in modern full-stack applications. This skill covers unit testing, integration testing, mocking strategies, and CI/CD integration.\n\n## Installation\n\n### NPM Installation\n\n```bash\nnpm install @ainative/skill-testing-patterns\n```\n\n### Manual Installation\n\n1. Clone or download this skill to your project's skills directory\n2. Reference it in your AINative Studio configuration\n\n## What This Skill Provides\n\n### Core Testing Principles\n\n- **Test-Driven Development (TDD)**: RED → GREEN → REFACTOR cycle\n- **Behavior-Driven Development (BDD)**: Given-When-Then pattern\n- **AAA Pattern**: Arrange, Act, Assert structure\n- **Coverage Goals**: Minimum 80% code coverage requirement\n\n### Framework Coverage\n\n#### Backend (Python/FastAPI)\n- pytest configuration and setup\n- Database testing with SQLAlchemy\n- API endpoint testing with TestClient\n- Async operation testing\n- Fixture management\n- Parametrized testing\n\n#### Frontend (TypeScript/React)\n- Vitest configuration\n- Component testing with React Testing Library\n- Hook testing\n- User interaction testing\n- Async operation testing\n- Snapshot testing\n\n### Testing Strategies\n\n1. **Unit Testing**\n   - Isolated function and class testing\n   - Mock external dependencies\n   - Fast execution (<1ms per test)\n   - High coverage of business logic\n\n2. **Integration Testing**\n   - Multi-component interaction testing\n   - Real database instances\n   - API endpoint workflows\n   - End-to-end user journeys\n\n3. **Mocking Patterns**\n   - External API mocking\n   - Database operation mocking\n   - File system mocking\n   - Third-party service mocking\n   - ZeroDB and AI service mocking\n\n4. **CI/CD Integration**\n   - GitHub Actions workflows\n   - Coverage reporting with Codecov\n   - Quality gates and branch protection\n   - Parallel test execution\n   - Performance testing\n\n## Reference Documentation\n\nThis skill includes comprehensive reference files:\n\n- **`SKILL.md`**: Main skill file with core concepts and quick reference\n- **`references/pytest-config.md`**: Complete pytest configuration guide\n- **`references/vitest-config.md`**: Vitest setup for React testing\n- **`references/mock-patterns.md`**: Mocking strategies for all dependencies\n- **`references/integration-tests.md`**: Integration and E2E testing patterns\n- **`references/ci-integration.md`**: GitHub Actions and CI/CD setup\n- **`references/test-examples.md`**: Real-world production test examples\n\n## Quick Start\n\n### For Backend (FastAPI + pytest)\n\n1. **Configure pytest** - See `references/pytest-config.md`\n\n```bash\n# Install dependencies\npip install pytest pytest-cov pytest-asyncio\n\n# Create pytest.ini\ncd backend\n# Copy configuration from pytest-config.md\n\n# Run tests\npytest --cov=src --cov-fail-under=80\n```\n\n2. **Write your first test**\n\n```python\ndef test_user_registration(client, db_session):\n    # ARRANGE\n    user_data = {\"email\": \"test@example.com\", \"password\": \"SecurePass123!\"}\n\n    # ACT\n    response = client.post(\"/api/auth/register\", json=user_data)\n\n    # ASSERT\n    assert response.status_code == 201\n    assert response.json()[\"email\"] == \"test@example.com\"\n```\n\n### For Frontend (React + Vitest)\n\n1. **Configure Vitest** - See `references/vitest-config.md`\n\n```bash\n# Install dependencies\nnpm install -D vitest @testing-library/react @testing-library/user-event jsdom\n\n# Create vitest.config.ts\n# Copy configuration from vitest-config.md\n\n# Run tests\nnpm run test:coverage\n```\n\n2. **Write your first test**\n\n```typescript\nimport { render, screen } from '@testing-library/react'\nimport { Button } from './Button'\n\ntest('renders button with text', () => {\n  // ARRANGE & ACT\n  render(<Button>Click Me</Button>)\n\n  // ASSERT\n  expect(screen.getByText('Click Me')).toBeInTheDocument()\n})\n```\n\n## Usage Examples\n\n### Testing API Endpoints\n\n```python\n@pytest.mark.integration\ndef test_create_and_retrieve_item(client, auth_headers):\n    # Create item\n    response = client.post(\"/api/items\", json={\n        \"name\": \"Test Item\",\n        \"price\": 29.99\n    }, headers=auth_headers)\n\n    assert response.status_code == 201\n    item_id = response.json()[\"id\"]\n\n    # Retrieve item\n    response = client.get(f\"/api/items/{item_id}\")\n    assert response.status_code == 200\n    assert response.json()[\"name\"] == \"Test Item\"\n```\n\n### Testing React Components\n\n```typescript\nimport userEvent from '@testing-library/user-event'\n\ntest('form submission', async () => {\n  const user = userEvent.setup()\n  const handleSubmit = vi.fn()\n\n  render(<LoginForm onSubmit={handleSubmit} />)\n\n  await user.type(screen.getByLabelText(/email/i), 'test@example.com')\n  await user.type(screen.getByLabelText(/password/i), 'password123')\n  await user.click(screen.getByRole('button', { name: /login/i }))\n\n  expect(handleSubmit).toHaveBeenCalledWith({\n    email: 'test@example.com',\n    password: 'password123'\n  })\n})\n```\n\n### Mocking External Services\n\n```python\n@pytest.fixture\ndef mock_openai():\n    with patch('openai.ChatCompletion.create') as mock:\n        mock.return_value = {\n            'choices': [{'message': {'content': 'AI response'}}]\n        }\n        yield mock\n\ndef test_ai_completion(mock_openai):\n    response = get_ai_response(\"Hello\")\n    assert response == \"AI response\"\n    mock_openai.assert_called_once()\n```\n\n## Integration with Other Skills\n\n### Works With\n\n- **mandatory-tdd**: Enforces TDD workflow and coverage requirements\n- **code-quality**: Ensures tests follow coding standards\n- **ci-cd-compliance**: Integrates tests into CI/CD pipeline\n- **database-schema-sync**: Tests database migrations and schema changes\n\n### Complementary Skills\n\nThis skill provides the testing implementation patterns that complement:\n- Backend API development workflows\n- Frontend component development\n- Database operation testing\n- Security testing practices\n\n## Best Practices\n\n### DO ✅\n\n- Write tests before implementation (TDD)\n- Use descriptive test names\n- Follow AAA pattern (Arrange, Act, Assert)\n- Test edge cases and error conditions\n- Mock external dependencies\n- Maintain ≥80% code coverage\n- Run tests in CI/CD pipeline\n\n### DON'T ❌\n\n- Skip tests for \"simple\" code\n- Write tests after implementation\n- Test implementation details\n- Share state between tests\n- Use sleep() for timing\n- Ignore flaky tests\n- Commit failing tests\n\n## Common Commands\n\n### pytest Commands\n\n```bash\npytest                          # Run all tests\npytest -v                       # Verbose output\npytest -x                       # Stop on first failure\npytest --lf                     # Run last failed\npytest --cov=src                # With coverage\npytest -m unit                  # Run unit tests only\npytest -k \"test_user\"           # Pattern matching\n```\n\n### Vitest Commands\n\n```bash\nnpm run test                    # Run tests\nnpm run test:watch              # Watch mode\nnpm run test:ui                 # UI mode\nnpm run test:coverage           # Generate coverage\n```\n\n## Coverage Requirements\n\nAll code must maintain **minimum 80% code coverage**:\n\n```bash\n# Backend\npytest --cov=src --cov-fail-under=80\n\n# Frontend\nnpm run test:coverage  # Configured in vitest.config.ts\n```\n\n## Troubleshooting\n\n### Common Issues\n\n**Tests pass locally but fail in CI**\n- Ensure environment parity\n- Check for timing/race conditions\n- Verify database state reset\n\n**Slow test execution**\n- Use pytest-xdist for parallel execution\n- Mock external dependencies\n- Optimize database fixtures\n\n**Flaky tests**\n- Identify root cause (timing, state, randomness)\n- Fix properly, don't just retry\n- Ensure test isolation\n\n## Contributing\n\nWhen adding new test patterns to this skill:\n\n1. Follow existing structure and format\n2. Include real-world examples\n3. Document best practices\n4. Keep files under 500 lines\n5. Test all examples before committing\n\n## License\n\nMIT License - See LICENSE file for details\n\n## Support\n\nFor issues, questions, or contributions:\n- GitHub Issues: https://github.com/ainative/skills/issues\n- Documentation: See reference files in this skill\n- Examples: See `references/test-examples.md`\n\n---\n\n**Version**: 1.0.0\n**Author**: AINative Studio\n**Tags**: testing, tdd, bdd, pytest, vitest, fastapi, react\n","readmeFilename":"README.md","_rev":"1-63095fab0cff3c5839403e560f9edc76"}