* Delete outdated development instructions for Next.js + Tailwind and Python; add comprehensive guidelines for PostgreSQL DBA, Angular, ASP.NET REST APIs, Azure Functions with TypeScript, Bicep, Blazor, CMake with vcpkg, C#, .NET MAUI, GenAIScript, Terraform for Azure, localization, and markdown standards. * Update documentation and prompts for consistency and clarity - Standardized description formatting in various markdown files to use single quotes. - Added error handling utility in update-readme.js for safer file operations. - Improved title extraction logic in update-readme.js to handle frontmatter more robustly. - Updated chat modes section in README to reflect new emoji and sorted chat mode links. - Cleaned up various instruction files for better readability and consistency. - Ensured all markdown files end with a newline for better compatibility with version control. * Remove standardize-frontmatter.js script * Add usage instructions for creating and switching chat modes in README.md * Update README.md generation script to enhance instructions and usage details for custom chat modes * Update README.md and update-readme.js for improved instruction clarity and consistency * Refactor README.md links and update readme script for improved clarity and consistency in instructions * Update README.md and update-readme.js for improved instruction clarity and consistency * Changing from a patch to regen approach for the readme * Bit more cleanup for how to show things in the readme * Adding missing description * Another missing description --------- Co-authored-by: Aaron Powell <me@aaron-powell.com>
2.2 KiB
2.2 KiB
| description |
|---|
| Best practices for writing JavaScript/TypeScript tests using Jest, including mocking strategies, test structure, and common patterns. |
Test Structure
- Name test files with
.test.tsor.test.jssuffix - Place test files next to the code they test or in a dedicated
__tests__directory - Use descriptive test names that explain the expected behavior
- Use nested describe blocks to organize related tests
- Follow the pattern:
describe('Component/Function/Class', () => { it('should do something', () => {}) })
Effective Mocking
- Mock external dependencies (APIs, databases, etc.) to isolate your tests
- Use
jest.mock()for module-level mocks - Use
jest.spyOn()for specific function mocks - Use
mockImplementation()ormockReturnValue()to define mock behavior - Reset mocks between tests with
jest.resetAllMocks()inafterEach
Testing Async Code
- Always return promises or use async/await syntax in tests
- Use
resolves/rejectsmatchers for promises - Set appropriate timeouts for slow tests with
jest.setTimeout()
Snapshot Testing
- Use snapshot tests for UI components or complex objects that change infrequently
- Keep snapshots small and focused
- Review snapshot changes carefully before committing
Testing React Components
- Use React Testing Library over Enzyme for testing components
- Test user behavior and component accessibility
- Query elements by accessibility roles, labels, or text content
- Use
userEventoverfireEventfor more realistic user interactions
Common Jest Matchers
- Basic:
expect(value).toBe(expected),expect(value).toEqual(expected) - Truthiness:
expect(value).toBeTruthy(),expect(value).toBeFalsy() - Numbers:
expect(value).toBeGreaterThan(3),expect(value).toBeLessThanOrEqual(3) - Strings:
expect(value).toMatch(/pattern/),expect(value).toContain('substring') - Arrays:
expect(array).toContain(item),expect(array).toHaveLength(3) - Objects:
expect(object).toHaveProperty('key', value) - Exceptions:
expect(fn).toThrow(),expect(fn).toThrow(Error) - Mock functions:
expect(mockFn).toHaveBeenCalled(),expect(mockFn).toHaveBeenCalledWith(arg1, arg2)