agentleFS
atuljha23atuljha23/holocron/skills/test-patterns/SKILL.md

test-patterns

Skill2 starsChanged 7 months agoMITView on GitHub

Reference for writing tests that actually protect against regressions — deep assertions over spies, table-driven tests, fixture hygiene, AAA structure. Use when writing, reviewing, or refactoring tests.

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 1 is distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

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

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

    If three tests set up the same validUser, extract it.

    line 59
  2. Distinctive:

    Avoid testing props/state.

    line 74
  3. Very specific:

    Use fixtures for setup, not class-level setUp.

    line 77
  4. Very specific:

    Avoid toHaveBeenCalled when you can check the effect instead.

    line 87

≈ “Common” is an estimate: at least 1 in 5 of the 213,895 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

Test patterns

A test's job is to fail when the behavior is wrong. Most mediocre tests pass whether the code is right or not.

Rules of thumb

Assert behavior, not implementation

Bad:

expect(spy).toHaveBeenCalled()

Good:

expect(response.body.status).toBe('completed')
expect(db.getUser(id).lastSeenAt).toBe(mockNow)

Spy-assertions fail on refactor and protect nothing. Behavior-assertions fail when the user-visible contract breaks.

One logical check per test

Many expect lines can add up to one assertion of one outcome — that's fine. What's not fine: one test that covers three unrelated behaviors so nobody can tell what broke when it fails.

Arrange-Act-Assert

it('rejects stale tokens', () => {
  // Arrange
  const token = signToken({ exp: yesterday() })
  // Act
  const result = verify(token)
  // Assert
  expect(result.ok).toBe(false)
  expect(result.reason).toBe('expired')
})

Table-driven when the shape repeats

test.each([
  ['empty',       '',         'required'],
  ['too short',   'ab',       'min_length'],
  ['has space',   'a b',      'invalid_char'],
  ['ok',          'alice',    null],
])('validateUsername(%s=%j)', (_, input, expected) => {
  expect(validateUsername(input).error).toBe(expected)
})

Fixtures > inline setup

If three tests set up the same validUser, extract it. If the setup is 20 lines, it's probably doing too much — mock less, use a real test DB.

Name tests as specs

The test name is a sentence the reader can understand without opening the code. it('rejects stale tokens') > it('test2').

Integration > unit for risk hotspots

Unit tests are great for pure logic. For "this endpoint returns the right data for this user" you want an integration test that hits a real database, a real router, and a real serializer. Mocks hide the bugs you actually ship.

Framework-specific pointers

Testing Library (React/Vue/Svelte)

  • Query by role/label first, getByTestId last.
  • userEvent over fireEvent.
  • Avoid testing props/state; test what the user sees.

Pytest

  • Use fixtures for setup, not class-level setUp.
  • parametrize for table tests.
  • -k and markers to target — CI can split suites.

Go

  • Table tests with subtests: t.Run(tt.name, func(t *testing.T) { ... }).
  • t.Cleanup over defer for teardown.

Jest/Vitest

  • describe.each + test.each for tables.
  • Avoid toHaveBeenCalled when you can check the effect instead.

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.