---
name: test-writer
description: Write meaningful automated tests for JavaScript/TypeScript (Vitest, Jest, Testing Library, Playwright) and Python (pytest) that follow the project's existing conventions. Use whenever the user asks to add tests, improve coverage, "bikin test", "buat unit test", test a function/component/endpoint, or after implementing a feature that has no tests.
title: test-writer
canonical_url: https://skilld.dev/gh/kiln-skills/kiln/test-writer
last_updated: 2026-11-12T03:45:56.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [scripts/detect_test_setup.sh](https://skilld.dev/api/skills-raw/kiln-skills/kiln/test-writer/scripts/detect_test_setup.sh).
>
> If the user asked to install this Skill, run `npx skilld install kiln-skills/kiln/test-writer`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/test-writer`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/kiln-skills/kiln/test-writer) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/test-writer`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/test-writer --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/test-writer --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# Test Writer

Good tests fail when behavior breaks and pass when it doesn't. They're not coverage padding.

## 1. Detect the setup — never assume

Run `scripts/detect_test_setup.sh` from the project root. It reports the framework, test command, and where existing tests live.

Then open 1–2 existing test files near the target code and copy their conventions: file naming (`*.test.ts` vs `*.spec.ts`, `test_*.py`), folder (`__tests__/`, `tests/`, colocated), imports, fixtures/factories, mocking style. Consistency beats your preferences.

If there is no test setup at all, propose one (Vitest for Vite/TS projects, Jest otherwise; pytest for Python) and ask before installing.

## 2. Decide what to test

Read the target code and list behaviors, not lines:
- **Happy path** — the main expected use
- **Edge cases** — empty input, zero, negative, very large, unicode, `null`/`None`/`undefined`, duplicate items
- **Error paths** — invalid input raises/rejects with the right error
- **Boundaries** — exactly at limits (`<` vs `<=`)
- **State/side effects** — what gets written, called, emitted

Prioritize by risk: business logic and data transforms first; trivial getters last or never.

## 3. Write the tests

Principles:
- One behavior per test, named as a sentence: `it("returns empty list when no users match")`, `def test_returns_empty_list_when_no_users_match()`.
- Arrange / Act / Assert, visibly separated.
- Assert on outputs and observable effects, not on internal implementation details.
- Mock only at the edges (network, time, filesystem, randomness, external APIs). Don't mock the code under test.
- Deterministic: freeze time (`vi.useFakeTimers()`, `freezegun`/`time-machine`), seed randomness, no real network.
- Use parametrization for input tables: `it.each` / `test.each`, `@pytest.mark.parametrize`.
- React components: Testing Library, query by role/label text (`getByRole("button", { name: /simpan/i })`), use `userEvent`, not implementation selectors.
- Python async: `pytest-asyncio` if the project uses it.
- API endpoints: FastAPI `TestClient` / Django test client / supertest for Express.

## 4. Prove the tests are real

1. Run the new tests — they must pass.
2. **Mutation check**: temporarily break the code under test (flip a condition, return early) and confirm at least one new test fails. Revert. A test that can't fail is worthless.
3. Run the whole related suite to make sure nothing else broke.

## 5. Report

Briefly list: which behaviors are now covered, anything deliberately skipped (and why), and any bugs the tests uncovered — report those to the user rather than silently "fixing" the test to match buggy behavior.
