Skip to main content

javascript-typescript-jest

Best practices for writing JavaScript/TypeScript tests using Jest, including mocking strategies, test structure, and common patterns.

소스 정보

저장소
github/awesome-copilot
최근 소스 활동
2026년 2월 24일 03:39
감지된 SKILL.md 언어
영어
스타
40,696
포크
5,266

설치 방법

기본적으로 소스를 먼저 확인하는 Prompt가 선택됩니다. 직접 명령으로 전환하거나 로컬 사본을 다운로드할 수도 있습니다.

소스 파일 검토

설치 여부를 결정하기 전에 SKILL.md와 SkillsMP에 표시된 보조 파일을 읽어 보세요.

SKILL.md 표시 중

SKILL.md
소스 지침 · 읽기 전용 미리보기
name
javascript-typescript-jest
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.ts` or `.test.js` suffix - 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()` or `mockReturnValue()` to define mock behavior - Reset mocks between tests with `jest.resetAllMocks()` in `afterEach` ### Testing Async Code - Always return promises or use async/await syntax in tests - Use `resolves`/`rejects` matchers 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 `userEvent` over `fireEvent` for 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)`
GitHub에서 보기