
A snapshot fixture uses a stable digest to detect byte changes in generated copy across runs. The short prefix keeps local fixture labels readable. Every change in the exact source bytes changes the input to the digest.
A small reproduction for the console
The example keeps the input to node:crypto outside the function, so it is possible to run the same transformation in a test. The comparison uses concrete data rather than the rendered component. If the application accepts a value from JSON, run the fixture after parsing that JSON to preserve the relevant types.
const crypto = require('node:crypto');
function evaluate(value) {
return crypto.createHash('sha256').update(value,'utf8').digest('hex').slice(0,12);
}
const samples = ["desk", "desk\n", "Desk"];
for (const sample of samples) console.log(evaluate(sample));Compare return values
| Input | Return value |
|---|---|
| 'desk' | '49be417ad740' |
| 'desk\n' | '23dc9af36aee' |
| 'Desk' | '210e51e01936' |
For 'desk', the observed output is '49be417ad740'. With 'desk\n', it becomes '23dc9af36aee'. Record both observations in the test that owns the public behaviour. A later refactor can swap the mechanics underneath it as long as the callers continue to receive the values they rely on.
The result for 'Desk' is text, '210e51e01936'. Compare the resulting characters directly in a test. If two outputs look identical on screen, printing a quoted representation can expose a trailing space, a line boundary or an escaped character.
Where this approach ends
Treat a prefix length as a collision tradeoff. Retain the full digest if a downstream protocol requires it.
Run one case twice through the real feature and look at both the displayed value and its stored representation. A component may coerce a result to text even when the implementation returns a text value. Keep a failing sample small enough that it can travel with a bug report, including the browser or Node version that produced it.
The boundary case most likely to be missed
Hash source text after applying the encoding and newline rules of the real file writer. A snapshot generator running on a different platform can produce a different cache name even when the visible content appears unchanged.
The same Node.js function was also run with '' / 'desk ' / 'ksed'. These additional inputs change the boundary without changing the implementation. An exception name in the table records where the operation rejects an input; a returned empty or zero value records a successful call. Choose deliberately which of those outcomes the surrounding feature should expose.
| Additional fixture | Result or exception |
|---|---|
| '' | 'e3b0c44298fc' |
| 'desk ' | '3b1696457376' |
| 'ksed' | '37b4328a9e5f' |
Node.js node:crypto API · MDN JavaScript language guide
Sources and further reading
Cover photograph: Rational R1000 Development System by Provided by Grady Booch https://twitter.com/Grady_Booch/status/1167324163255623680. CC BY 4.0. Cropped and typeset for this cover. The photograph illustrates the topic.
