agentleFS
MindrallyMindrally/skills/jest/SKILL.md

jest

Skill275 starsChanged 36 days agoApache-2.0View on GitHub

Jest testing best practices for JavaScript and TypeScript applications, covering test structure, mocking, and assertion patterns.

4 other projects use a version of this.

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

19 rules. 0 are common, 10 are distinctive, and 9 are very specific to this project.

  • 0

    Common

    Most of the 4 edited versions we read keep these.

  • 10

    Distinctive

    This author's own choices. Worth a look.

  • 9

    Very specific

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

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use descriptive test names that clearly explain expected behavior

    line 16
  2. Distinctive:

    Follow the Arrange-Act-Assert (AAA) pattern in each test

    line 18
  3. Distinctive:

    Keep tests focused on a single behavior or outcome

    line 19
  4. Very specific:

    Use beforeEach and afterEach for test isolation

    line 22
  5. Very specific:

    Use beforeAll and afterAll for expensive setup that can be shared

    line 23
  6. Very specific:

    Use jest.mock() for module mocking

    line 27
  7. Very specific:

    Use jest.fn() for function mocks

    line 28
  8. Very specific:

    Use jest.spyOn() when you need to track calls but keep implementation

    line 29
  9. Distinctive:

    Avoid over-mocking: test real behavior when feasible

    line 31
  10. Distinctive:

    Use the most specific matcher available

    line 34
  11. Very specific:

    Prefer toEqual for object comparison, toBe for primitives

    line 35
  12. Very specific:

    Use toMatchSnapshot sparingly and with meaningful names

    line 36
  13. Distinctive:

    Always return promises or use async/await in async tests

    line 40
  14. Very specific:

    Use waitFor from testing libraries for async assertions

    line 41
  15. Distinctive:

    Test edge cases and error conditions

    line 46
  16. Very specific:

    Use --coverage flag to track coverage metrics

    line 47
  17. Distinctive:

    Keep tests DRY but readable (prefer clarity over brevity)

    line 50
  18. Distinctive:

    Test behavior, not implementation details

    line 51
  19. Distinctive:

    Use factories or builders for test data creation

    line 53

“Common” is measured: kept by at least 6 in 10 of the 4 edited versions of this file we could read. “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

Jest Testing Best Practices

You are an expert in JavaScript, TypeScript, and Jest testing.

Core Principles

Test Structure

  • Use descriptive test names that clearly explain expected behavior
  • Organize tests using describe blocks for logical grouping
  • Follow the Arrange-Act-Assert (AAA) pattern in each test
  • Keep tests focused on a single behavior or outcome

Setup and Teardown

  • Use beforeEach and afterEach for test isolation
  • Use beforeAll and afterAll for expensive setup that can be shared
  • Clean up any side effects in teardown hooks

Mocking

  • Use jest.mock() for module mocking
  • Use jest.fn() for function mocks
  • Use jest.spyOn() when you need to track calls but keep implementation
  • Clear mocks between tests with jest.clearAllMocks()
  • Avoid over-mocking: test real behavior when feasible

Assertions

  • Use the most specific matcher available
  • Prefer toEqual for object comparison, toBe for primitives
  • Use toMatchSnapshot sparingly and with meaningful names
  • Include negative test cases (what should NOT happen)

Async Testing

  • Always return promises or use async/await in async tests
  • Use waitFor from testing libraries for async assertions
  • Set appropriate timeouts for long-running tests

Coverage

  • Aim for meaningful coverage, not just high percentages
  • Test edge cases and error conditions
  • Use --coverage flag to track coverage metrics

Best Practices

  • Keep tests DRY but readable (prefer clarity over brevity)
  • Test behavior, not implementation details
  • Make tests deterministic (no random data without seeding)
  • Use factories or builders for test data creation

Example Patterns

describe('fetchUser', () => {
  it('should return user data when API call succeeds', async () => {
    const mockUser = { id: 1, name: 'John' };
    jest.spyOn(api, 'get').mockResolvedValue(mockUser);

    const result = await fetchUser(1);

    expect(result).toEqual(mockUser);
    expect(api.get).toHaveBeenCalledWith('/users/1');
  });

  it('should throw error when API call fails', async () => {
    jest.spyOn(api, 'get').mockRejectedValue(new Error('Not found'));

    await expect(fetchUser(999)).rejects.toThrow('Not found');
  });
});

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.