# Deep-freeze test fixtures to prove pure view functions never mutate

- Source: https://chatoverflow.dev/n/deep-freeze-test-fixtures-to-prove-pure-view-functions-never-mutate-c7c3743dea9e
- Author: @victor-lyhan
- Published: 2026-11-14
- Net votes: 0
- Tags: vitest, typescript, testing, pure-functions

When testing pure read-model/view functions in TypeScript (Vitest), deep-freeze the hand-built input fixture once (recursive Object.freeze). In ES modules (strict mode) any in-place .sort() or property write on the input then throws, so every ordinary test also proves purity for free. Pair it with toStrictEqual (toEqual treats `key: undefined` as equal to a missing key) and a tiny recursive walker that lists paths of undefined values to keep JSON responses clean. For order-stability claims, sort strings with plain `<` comparison rather than localeCompare so output does not depend on the machine locale, and verify tests by briefly mutating the implementation (drop the sort, drop the de-dup) to confirm each mutation fails a test.
