Spy4js is a standalone JavaScript and TypeScript testing spy framework designed for integration with test runners like Vitest and Jest. It provides a robust API for creating and managing spies, focusing on test readability, detailed error messages, and efficient serialization of call arguments. The package aims to offer an intuitive alternative or supplement to the built-in spying capabilities of popular test frameworks. The current stable version is 5.0.0, released in September 2025. While release cadence can be irregular, significant updates (like the TypeScript migration in v3.0.0) introduce notable breaking changes. Key differentiators include an API optimized for readability, enhanced error reporting with detailed comparisons, and features like customizable behavior and module mocking capabilities for both CommonJS and ES Modules.
npm install spy4jsVerified import paths — ran on the pinned version, not inferred.
Demonstrates initializing a spy, simulating calls, and performing basic assertions like checking call count and arguments, and mocking an existing object's method.
Remove the `new` keyword when creating a spy instance. Change `new Spy()` to `Spy()`.
To check if a spy was not called at all, use `spy.wasNotCalled()`. If you need to check for specific call arguments, provide them to `spy.hasCallHistory(...)`.
Evaluate `spy4js` based on its specific benefits (readability, detailed error messages, unique features) rather than as a fundamental dependency for spying. It's intended as an alternative or complementary tool.
Be aware of this configuration option when debugging test failures related to call order. If enabling it, ensure your tests explicitly account for the exact sequence of expected calls.
Remove the `new` keyword. Instantiate the spy by calling `Spy()` directly, e.g., `const mySpy = Spy();`.
If you intend to check if the spy was never called, use `spy.wasNotCalled()`. If you want to check for specific call patterns, provide arguments to `spy.hasCallHistory(...)`.
Ensure that your module mocking strategy aligns with your project's module system. For ES Modules (e.g., in Vitest), use `await import()` with `vi.mock()`. For CommonJS (e.g., in Jest without ESM enabled), use `require()`.
No dependency data recorded yet.