Awesome-copilot javascript-typescript-jest
Best practices for writing JavaScript/TypeScript tests using Jest, including mocking strategies, test structure, and common patterns.
install
source · Clone the upstream repo
git clone https://github.com/github/awesome-copilot
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/github/awesome-copilot "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/javascript-typescript-jest" ~/.claude/skills/github-awesome-copilot-javascript-typescript-jest && rm -rf "$T"
manifest:
skills/javascript-typescript-jest/SKILL.mdsource content
Test Structure
- Name test files with
or.test.ts
suffix.test.js - Place test files next to the code they test or in a dedicated
directory__tests__ - 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
for module-level mocksjest.mock() - Use
for specific function mocksjest.spyOn() - Use
ormockImplementation()
to define mock behaviormockReturnValue() - Reset mocks between tests with
injest.resetAllMocks()afterEach
Testing Async Code
- Always return promises or use async/await syntax in tests
- Use
/resolves
matchers for promisesrejects - 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
overuserEvent
for more realistic user interactionsfireEvent
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)