agentleFS
Mingz6Mingz6/copilot-dev-os/skills/javascript-typescript-jest/SKILL.md

javascript-typescript-jest

Skill1 starsChanged 35 days agoMITView on GitHub

Jest/Vitest testing patterns for JS/TS — mocking, spies, snapshots, async tests, structure. Use when: 'write a jest test', 'write a vitest test', 'mock this module', 'spy on a function', 'snapshot test', 'set up jest', 'fix a flaky test', 'write tests', 'unit test this', 'add tests for', or reviewing existing Jest/Vitest tests. Vitest has a compatible API — patterns apply to both.

2 other projects use a version of this, 2 of them edited.

Adopted by

Most recently taken up by

The file

Show its rules and text

We split this skill into the rules it gives an agent

14 rules. About 0 are common, 7 are distinctive, and 7 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈7

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 7

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Place test files next to the code they test or in a dedicated __tests__ directory

    line 14
  2. Distinctive:

    Use descriptive test names that explain the expected behavior

    line 15
  3. Distinctive:

    Use nested describe blocks to organize related tests

    line 16
  4. Very specific:

    Follow the pattern: describe('Component/Function/Class', () => { it('should do something', () => {}) })

    line 17
  5. Very specific:

    Use jest.mock() for module-level mocks

    line 21
  6. Very specific:

    Use jest.spyOn() for specific function mocks

    line 22
  7. Very specific:

    Use mockImplementation() or mockReturnValue() to define mock behavior

    line 23
  8. Distinctive:

    Always return promises or use async/await syntax in tests

    line 27
  9. Very specific:

    Use resolves/rejects matchers for promises

    line 28
  10. Distinctive:

    Use snapshot tests for UI components or complex objects that change infrequently

    line 32
  11. Distinctive:

    Keep snapshots small and focused

    line 33
  12. Distinctive:

    Use React Testing Library over Enzyme for testing components

    line 37
  13. Distinctive:

    Test user behavior and component accessibility

    line 38
  14. Very specific:

    Use userEvent over fireEvent for more realistic user interactions

    line 40

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Use when

  • Writing or reviewing Jest tests for JS/TS code
  • Setting up mocks, spies, or snapshot tests

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)

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.