$ show sinonjs/sinon
Sinon.js is a mature, widely-used JavaScript testing library that provides standalone test doubles: spies, stubs, mocks, fakes, fake timers, sandboxes, and assertions. It is designed to be test-framework agnostic and works in both Node.js and browser environments. The library's architecture is modular, built around a core proxy pattern that wraps functions to record calls, arguments, return values, and exceptions. Behaviors (return values, throws, callbacks, etc.) are defined separately and can be chained per invocation. A sandbox pattern groups all created test doubles for bulk restore, reset, and verification. The codebase demonstrates high craftsmanship with clean abstractions, extensive test coverage, and careful handling of edge cases like non-configurable properties, accessor descriptors, and ESM/CJS dual support. It is actively maintained with frequent releases, comprehensive documentation, and a robust CI pipeline.
The LLM identified 5 files worth highlighting for reuse:
Core module that creates proxy functions wrapping any target function. It records all calls (arguments, return values, exceptions, this context) and provides methods like `calledWith`, `returned`, `threw` via delegation to individual call records. The proxy pattern is the foundation for spies, stubs, and fakes. It is self-contained and could be extracted to build any call-tracking wrapper.
Defines how a stub or fake responds to invocations. Behaviors include returning values, throwing errors, calling callbacks, resolving/rejecting promises, and yielding. Behaviors can be assigned per call index (e.g., `onFirstCall`, `onCall(2)`) and are invoked when the stub is called. This module is the engine behind stub/fake behavior and is reusable for any mock-like system.
Central sandbox class that manages collections of spies, stubs, mocks, fakes, and fake timers. Provides methods to create test doubles, replace properties, and use fake timers. The sandbox tracks all created doubles and can restore, reset, or verify them in bulk. This pattern is essential for test isolation and cleanup, and can be reused in any testing framework.
A utility for copying properties from one object to another, with careful handling of property descriptors (including accessor properties and non-writable `name`). It supports both own and inherited properties and is used throughout Sinon to mix behavior onto proxy objects. This is a standalone, well-tested utility that can be reused in any project needing safe property extension.
Creates a promise that can be externally resolved or rejected, exposing its status (pending, resolved, rejected) and resolved value/reason. This is useful for testing asynchronous code without relying on real timers. The implementation is self-contained and demonstrates a clean deferred promise pattern that can be extracted for any project needing controllable promises.