npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rimbu/spy

v0.7.1

Published

Test utilities for spying, stubbing and mocking functions, objects and classes in TypeScript

Downloads

4

Readme

npm version Deno

Licence

@rimbu/spy

This package supports testing through utilities that can create spies and mocks for functions, objects, classes. It is still in experimental phase.

Motivation

With frameworks like Deno and ES Modules it becomes much harder/impossible to mock modules like for example Jest does. Deno recommends to use specific ways to expose dependencies so that they can be replaced without mocking modules.

Alternatives like vitest do offer support for ES Modules, so if you like (to keep) this way of testing, that may be a viable option.

It is important to note that @rimbu/spy is aimed at testing without module mocking. This means that it is not possible to replace functions inside of other files/modules. The only way is to ensure the dependencies are in an object that is passed into the code that depends on it. This object can then be spied/mocked and passed into the code.

One motivation to use an external spying/mocking framework is to be independent of a specific testing framework, so that the pain of switching becomes much less if one decides to switch.

A popular external spying/mocking framework is Sinon JS, which offers probably most of the functionality available in this package. Like @rimbu/spy it is also aimed at spying/mocking without module mocking.

The @rimbu/spy package however offers a more simple and minimal API that is easy to learn and use. It also takes care that types are consistent with their original implementations.

It depends on preference and also use case coverage whether this package offers a full alternative. Please feel free to try it out and provide feedback, and don't forget to create issues if you find anything that is not working correctly.

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

For yarn:

yarn add @rimbu/spy

For npm:

npm i @rimbu/spy

For bun:

bun add @rimbu/spy

Deno

For Deno, the following approach is recommended:

In the root folder of your project, create or edit a file called import_map.json with the following contents (where you should replace x.y.z with the desired version of Rimbu):

{
  "imports": {
    "@rimbu/": "https://deno.land/x/[email protected]/"
  }
}

Note: The trailing slashes are important!

In this way you can use relative imports from Rimbu in your code, like so:

import { Spy } from '@rimbu/spy/mod.ts';

To run your script (let's assume the entry point is in src/main.ts):

deno run --import-map import_map.json src/main.ts

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

Arvid Nicolaas

Contributing

Feel very welcome to contribute to further improve Rimbu. Please read our Contributing guide.

Contributors

Made with contributors-img.

License

Licensed under the MIT License, Copyright © 2020-present Arvid Nicolaas.

See LICENSE for more information.