JavaScript TypeScript Jest

JavaScript TypeScript Jest

Sets out best practices for writing Jest tests in JavaScript and TypeScript: structure, mocking, async code, snapshots and React components.

Skill files1 file
SKILL.md45 lines
1
---
2
name: javascript-typescript-jest
3
description: 'Best practices for writing JavaScript/TypeScript tests using Jest, including mocking strategies, test structure, and common patterns.'
4
---
5
6
### Test Structure
7
- Name test files with `.test.ts` or `.test.js` suffix
8
- Place test files next to the code they test or in a dedicated `__tests__` directory
9
- Use descriptive test names that explain the expected behavior
10
- Use nested describe blocks to organize related tests
11
- Follow the pattern: `describe('Component/Function/Class', () => { it('should do something', () => {}) })`
12
13
### Effective Mocking
14
- Mock external dependencies (APIs, databases, etc.) to isolate your tests
15
- Use `jest.mock()` for module-level mocks
16
- Use `jest.spyOn()` for specific function mocks
17
- Use `mockImplementation()` or `mockReturnValue()` to define mock behavior
18
- Reset mocks between tests with `jest.resetAllMocks()` in `afterEach`
19
20
### Testing Async Code
21
- Always return promises or use async/await syntax in tests
22
- Use `resolves`/`rejects` matchers for promises
23
- Set appropriate timeouts for slow tests with `jest.setTimeout()`
24
25
### Snapshot Testing
26
- Use snapshot tests for UI components or complex objects that change infrequently
27
- Keep snapshots small and focused
28
- Review snapshot changes carefully before committing
29
30
### Testing React Components
31
- Use React Testing Library over Enzyme for testing components
32
- Test user behavior and component accessibility
33
- Query elements by accessibility roles, labels, or text content
34
- Use `userEvent` over `fireEvent` for more realistic user interactions
35
36
## Common Jest Matchers
37
- Basic: `expect(value).toBe(expected)`, `expect(value).toEqual(expected)`
38
- Truthiness: `expect(value).toBeTruthy()`, `expect(value).toBeFalsy()`
39
- Numbers: `expect(value).toBeGreaterThan(3)`, `expect(value).toBeLessThanOrEqual(3)`
40
- Strings: `expect(value).toMatch(/pattern/)`, `expect(value).toContain('substring')`
41
- Arrays: `expect(array).toContain(item)`, `expect(array).toHaveLength(3)`
42
- Objects: `expect(object).toHaveProperty('key', value)`
43
- Exceptions: `expect(fn).toThrow()`, `expect(fn).toThrow(Error)`
44
- Mock functions: `expect(mockFn).toHaveBeenCalled()`, `expect(mockFn).toHaveBeenCalledWith(arg1, arg2)`
45

© GitHub, Inc. · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 14,621 prompts

More by @OpenSourceBuffGuy

See all
Sea turtle carrying floating film studio island prompt image

Sea turtle carrying floating film studio island

Draws a giant sea turtle carrying a floating island film studio through a starry sky, where fairies polish rough film strips into golden film.

image-prompt, nano-banana, 3d-render, cartoon, fantasy-world, creatures, animals, whimsical, dreamy, cinematic, dramatic-lighting, vibrant-colors, bokeh, miniature, widescreen, night-scene, illustration, concept-art, surreal, storytelling, clay-3d

Desert buggy escapes drones past industrial outpost prompt image

Desert buggy escapes drones past industrial outpost

Builds a 15-second desert chase video where a racer in an armored buggy escapes drones past an industrial outpost, using five reference images with set jobs.

video-prompt, hailuo, action, multi-shot, timed-beats, camera-movement, reference-image, character-consistency, negative-prompt, cinematic, sci-fi, cars, landscape, aerial-view, tracking-shot, photorealistic, sound-effects, structured-output, long-form, widescreen

Cozy Pixel Art Farm Island Village prompt image

Cozy Pixel Art Farm Island Village

Draws a cozy 16-bit pixel-art farming village across six small islands joined by bridges, with exact counts for buildings, animals and crops.

image-prompt, gpt-image, pixel-art, isometric, game-assets, concept-art, maps, worldbuilding, cozy, vibrant-colors, ocean, landscape, wide-shot, widescreen, game-development, video-games, structured-output, whimsical, summer, high-angle, aerial-view, animals, negative-prompt, cityscape

1 view

Reviews

Loading reviews…

Discussion

Loading comments…