What a change must prove, and how a test case is written. These apply to every package in the repository.
Each package documents its own suites and commands: the engine's are in hyperformula/dev-docs/TESTING.md.
- Every change to
hyperformula/src/needs tests inhyperformula/test/. This is part of the definition of done, not a suggestion. - Bug fix: at least one test that reproduces the bug — it must fail against the unfixed code. Write it first and watch it fail.
- New feature: a set of tests that describe the feature precisely enough to serve as its specification. The suite's size is proportional to the spec: a few cases per spec row, not hundreds of near-duplicates.
- Cover more than the happy path: boundary values, empty and invalid input, error results, and interaction with related features. Cover every variant: both axes, both directions, each match or search mode, a non-default configuration.
- Test the positive paths, not only rejections; for example, granted features still work. Test the general paths, not only the fast ones; for example, composing cached aggregates.
- Functions the task names but leaves unchanged get regression tests.
- Pin silent-failure mechanisms with tests: warnings, call ordering, defensive copies.
- No tests for code that doesn't need them.
docs/, thescript/directories and vendored upstream code, such ashyperformula/src/license/handsontable-license-key-parser/, are not tested.
it('returns the divisor sign for arguments with opposite signs', () => {
const engine = HyperFormula.buildFromArray([['=MOD(-3, 12)']])
expect(engine.getCellValue(adr('A1'))).toBe(9)
})- One assertion per test case. Each case is very simple and focused. Split rather than adding a second
expect. - No control flow in a test case. No loops, no conditionals. A parameterised loop hides which input failed; write the cases out.
- Name the case after the behaviour it pins, not after the function under test: "returns
#VALUE!when the range is empty", not "test SUMIFS". - Build the smallest engine that exhibits the behaviour. A two-cell array beats a realistic sheet.
- Assert through the public API —
getCellValue,getCellFormula,getSheetValues— not through internals. - No duplicated tests or helpers across files; each file has one clear topic.
- Tests are deterministic and isolated:
- no wall-clock bounds;
- global state restored, for example language packs;
- no output on the real console.
- Use real inputs where they exist, for example real files, not synthetic ones. Build licence keys in the format real keys are issued in today, with the vendored key reader's own encoding and checksum, rather than writing key strings by hand. Never put a real customer's key in a test (see
SENSITIVE-INFORMATION.md). - Lower test-only constants so scenarios stay small, for example a configurable threshold.
- A test must prove intended behaviour. Never relax an assertion, widen a matcher, or skip a case to turn a run green — if a test is red, the default assumption is that the code is wrong.
Before requesting a review, ask which further tests would be valuable and add the ones that protect against realistic regressions.
A test that passes without proving anything is worse than no test: it occupies the space where the real test would have gone, and it makes the next reader believe the behaviour is covered.
Write the case from the requirement, not from the implementation. Reading the implementation first and then writing a test that mirrors it produces a test that passes for any implementation, including the wrong one.
When a test is red, the default assumption is that the code is wrong. Change the test only when you can state, in one sentence, why its expectation was wrong — and that sentence must be about the specification, not about the effort of fixing the code.
- Relaxing an assertion: an exact value to
toBeCloseTo, a specific error to "some error",toEqualtotoContain. - Deleting the assertion that fails and keeping the ones that pass.
- Adding
.skipor.todo, or commenting out a case that used to run. - Widening a matcher until every implementation passes.
- Catching the error the code should not be throwing, and asserting that it was caught.
- Mocking the unit under test, or mocking so deeply that only the mock is exercised.
- Asserting that a call "does not throw" when the requirement is a specific returned value.
- Changing the input until the current implementation happens to be right.
An assertion that is weakened or deleted for a valid reason has that reason recorded in the commit that changes it.
"This test fails and I do not yet know why" is a useful report. A green run that hides it is not.
These execute code and prove nothing. Assert the value the specification names.
expect(engine.getCellValue(adr('A1'))).toBeDefined() // any value passes
expect(() => engine.setCellContents(...)).not.toThrow() // any non-throwing bug passes
expect(result).toBeTruthy() // 1, 'x', and [] all passNever claim a test passes without having run it, and never claim a fix works because the reasoning is sound.