The term comes from espionage: a spy observes without acting, records without judging. The software sense was formalised by Meszaros in 2007.
Espionage uses "spy" for an operative who watches and reports. The software sense borrowed the metaphor in the mid-2000s as mocking frameworks matured and developers needed a double that observed rather than asserted. Meszaros' xUnit Test Patterns (2007) formalised it as one of five doubles; Fowler's Mocks Aren't Stubs (2007) placed it in the taxonomy; Sinon.js (2011) made it a first-class primitive in JavaScript testing, and Jest (2014) followed.
2007 — Meszaros formalises Test Spy in xUnit Test Patterns
2007 — Fowler's Mocks Aren't Stubs positions spies in the taxonomy
2011 — Sinon.js ships spies, stubs and mocks as distinct primitives
2014 — Jest introduces jest.fn() with spy semantics
2020 — Mockito and pytest consolidate spy patterns in Java/Python
2024 — Mozilla testing guidelines recommend spies over strict mocks
A Test Spy is a double that captures the indirect output of the system under test so that the test can verify the indirect output.
— Gerard Meszaros, *xUnit Test Patterns*, 2007