@rimbu/spy
v0.7.5
Published
Test utilities for spying, stubbing and mocking functions, objects and classes in TypeScript
Maintainers
Readme
@rimbu/spy
Welcome to @rimbu/spy! This package provides utilities for creating spies and mocks for functions, objects, and classes, aimed at enhancing your testing experience. Note that it is currently in the experimental phase.
Motivation
Testing frameworks like Deno and ES Modules present challenges for module mocking, unlike Jest. Deno recommends exposing dependencies in a way that allows replacement without module mocking.
While alternatives like Vitest support ES Modules and may be a viable option for those who prefer module mocking, @rimbu/spy focuses on testing without module mocking. This means you cannot replace functions inside other files/modules directly. Instead, dependencies should be encapsulated in an object passed into the dependent code, which can then be spied/mocked.
Key Benefits
- Framework Independence: Using an external spying/mocking framework reduces dependency on a specific testing framework, easing the transition if you decide to switch.
- Minimal and Simple API:
@rimbu/spyoffers a straightforward API that is easy to learn and use, ensuring type consistency with original implementations. - Alternative to Sinon JS: While Sinon JS provides extensive functionality,
@rimbu/spyoffers a simpler, more minimalistic approach.
Why Use @rimbu/spy?
- No Module Mocking: Ideal for scenarios where module mocking is not feasible or desired.
- Consistent Types: Ensures that types remain consistent with their original implementations.
- Ease of Use: Designed to be easy to learn and integrate into your testing workflow.
Feedback and Contributions
We encourage you to try out @rimbu/spy and provide feedback. If you encounter any issues or have suggestions for improvement, please don't hesitate to create issues on our repository.
Docs
Full documentation is still to be done. To read more about Rimbu, please visit the Rimbu Docs, or directly see the Rimbu Spy API Docs.
Or Try Out Rimbu in CodeSandBox.
Installation
Compabitity
Yarn / npm / Bun / Deno
For yarn:
yarn add @rimbu/spy
For npm:
npm i @rimbu/spy
For bun:
bun add @rimbu/spy
For deno:
deno add npm:@rimbu/spy
Usage
import { Spy } from '@rimbu/spy';
const spyConsole = Spy.obj(console, {
log: () => console.log('mocked'),
});
spyConsole.warn("warning");
// => behaves as normal, logs "warning"
spyConsole.log("hello", "world);
// => logs "mocked"
spyConsole[Spy.META].nrCalls; // => 2
spyConsole.log.nrCalls; // => 1
spyConsole.warn.calls[0]; // => ["warning"]
spyConsole[Spy.META].callSequence;
// => [["warn", "warning"], ["log", "hello", "world"]]Author
Created and maintained by Arvid Nicolaas.
Contributing
We welcome contributions! Please read our Contributing guide.
Contributors
Made with contributors-img.
License
This project is licensed under the MIT License. See the LICENSE for details.
