DRY vs DAMP and KISS
DRY demands one place for every piece of knowledge, while DAMP says a test should read on its own even at the cost of repetition. Hence the split: DRY for infrastructure (locators, steps, fixtures) and DAMP for the test body, where inputs and expectations stay visible. Extract what changes together, not merely identical lines. KISS adds that abstractions are not built in advance and branching logic is kept out of tests.
Patterns · Principles
To understand what a test checks you need to open three files. Where is the excess DRY?
The test “transfer limit” is one line, runScenario(page, SCENARIOS[3]). Duplication is zero, but to learn what exactly is checked you open the helper and the array and count an index. When such a test fails the report says “limit” and nothing else. This is the case where DRY went too far. The lesson is about where repetition hurts and where it helps.
How it works and why. DRY (Don't Repeat Yourself) demands that every fact has one place in the system. The word “fact” matters: it is not about identical lines but about identical knowledge. The locator of the “Confirm” button is knowledge about the markup, and it should live in one place. But that the limit test uses the amount 50,001 is not repeated knowledge, it is an input value, the substance of this test. DAMP (Descriptive And Meaningful Phrases) says a test must read on its own, even at the cost of repetition. Hence the split: DRY for infrastructure, DAMP for the test body.
What goes where
Example: before and after. Before (too DRY):
test('limit', async ({ transferPage }) => {
await runScenario(transferPage, SCENARIOS[3]);
});
To understand what is checked you must open two files. After (DAMP): the three steps and the expectation sit in the test itself, while locators and buttons stay in transferPage.
test('blocks a transfer above the daily limit', async ({ transferPage }) => {
await transferPage.fill('UA002', 50_001);
await transferPage.confirm();
await expect(transferPage.error).toHaveText('Daily limit exceeded');
});
Parametrisation. A loop over an array of cases with test() inside is also fine, provided each case has a clear name and visible data. Two marks:
const cases = [
1 { name: 'blocks above the daily limit', amount: 50_001, error: 'Daily limit exceeded' },
{ name: 'rejects zero', amount: 0, error: 'Amount must be positive' },
{ name: 'rejects negative', amount: -5, error: 'Amount must be positive' },
];
for (const c of cases) {
2 test(c.name, async ({ transferPage }) => {
await transferPage.fill('UA002', c.amount);
await transferPage.confirm();
await expect(transferPage.error).toHaveText(c.error);
});
}
- Each case describes data and expectation side by side: in the report it is a separate test with its own name.
- The loop body is linear: no
ifand branches, so all the cases behave the same.
KISS and YAGNI. Keep It Simple: do not build an abstraction “for the future”. Three tests with the same two lines are not yet a reason for a new layer: first the rule of three, and only when knowledge is duplicated, not a coincidence. Logic in tests (if, loops with conditions) is a signal too: a test must be linear, otherwise it is hard to say what it checks.
// logic in a test: which path runs depends on the state
if (await banner.isVisible()) {
await banner.getByRole('button', { name: 'Close' }).click();
}
// better: the state is fixed by a precondition, the test is linear
await expect(banner).toBeHidden();
Limits and alternatives. The criterion for extraction is simple: if a change in one place must change all the copies, it is shared knowledge and must not be duplicated (DRY). If different tests repeat a line because each needs its own value, keep the repetition (DAMP). A helper that hides expectations risks becoming a “black box”: it needs a clear name, and the check stays in the test.
Too DRY versus DAMP
Data hidden in a table
To understand what the test checks you open two files and count an index.
Data visible in the test
The name, the input and the expectation read together. Only what is the substance of the test repeats.
Common mistakes.
- Extracting everything repeated into a helper, including the inputs and expectations: the test stops being readable and a failure does not explain itself.
- Merging different tests into one parametrised “universal” one with flags: logic appears in the test.
- An abstraction after two repetitions: apply the rule of three and first make sure it is one piece of knowledge.
- Copying locators into every test for “readability”: that is no longer DAMP but duplication of knowledge about the markup.
- A loop with
test()without unique names: the cases cannot be told apart in the report.
Check yourself
Which part of a test should repeat (DAMP) and which live in one place (DRY)? Give an example of each.
Answer
DRY: the locator of the “Confirm” button, the login steps, a data factory: knowledge that changes together. DAMP: the amount 50,001 and the expected message “Daily limit exceeded”: the input and the result that are the substance of the test.
A test has if (await banner.isVisible()) close the banner. Why is it a problem and what to do?
Answer
The execution path depends on the state, so the test is non-deterministic and it is unclear what it checks. Fix the state with a precondition (a consent cookie or a fixture) and then assert that the banner is absent.