chai-spies vs jest vs proxyquire vs rewire vs sinon vs testdouble
JavaScript Testing Libraries
chai-spiesjestproxyquirerewiresinontestdoubleSimilar Packages:

JavaScript Testing Libraries

JavaScript testing libraries are tools that facilitate the process of writing and executing tests for JavaScript code. They provide various functionalities such as mocking, spying, and assertions to ensure that the code behaves as expected. These libraries help developers maintain code quality, catch bugs early, and ensure that changes do not break existing functionality. Each library has its own strengths and use cases, making them suitable for different testing scenarios in web development.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
chai-spies013287.1 kB142 years agoMIT
jest045,3036.32 kB2345 months agoMIT
proxyquire02,746-127 years agoMIT
rewire03,09424.4 kB516 months agoMIT
sinon09,7622.22 MB522 months agoBSD-3-Clause
testdouble01,429465 kB352 years agoMIT

Feature Comparison: chai-spies vs jest vs proxyquire vs rewire vs sinon vs testdouble

Spying and Mocking

  • chai-spies:

    Chai Spies provides a straightforward way to create spies for functions, allowing you to track calls, arguments, and return values without modifying the original function. It is lightweight and integrates seamlessly with Chai assertions.

  • jest:

    Jest includes built-in mocking capabilities, allowing you to easily create mock functions and modules. You can automatically mock dependencies and track calls, making it an all-in-one solution for testing.

  • proxyquire:

    Proxyquire allows you to override dependencies in your tests, enabling you to replace modules with mocks. This is particularly useful for isolating the module under test and controlling its environment.

  • rewire:

    Rewire enables you to access and modify private variables and functions within a module, allowing for more granular control during testing. This is useful for testing internal logic without exposing it publicly.

  • sinon:

    Sinon excels in creating spies, mocks, and stubs, providing a comprehensive API for tracking function calls and controlling behavior. It is highly configurable and works well with any testing framework.

  • testdouble:

    Testdouble offers a minimalistic approach to creating test doubles, focusing on simplicity and readability. It allows you to create mocks and stubs with a clean and intuitive API.

Integration with Testing Frameworks

  • chai-spies:

    Chai Spies is designed to work with the Chai assertion library, making it easy to integrate into existing test suites that use Mocha or other frameworks that support Chai.

  • jest:

    Jest is a standalone testing framework that includes everything you need to write and run tests, making it easy to set up and use without additional configuration.

  • proxyquire:

    Proxyquire can be used alongside any testing framework, such as Mocha or Jasmine, allowing you to integrate it into your existing test setup without issues.

  • rewire:

    Rewire can be used with any testing framework, providing flexibility in how you structure your tests while allowing for private variable access.

  • sinon:

    Sinon is framework-agnostic and can be used with any testing library, making it a versatile choice for adding spying and mocking capabilities to your tests.

  • testdouble:

    Testdouble is also framework-agnostic and can be integrated into any testing setup, providing a clean API for creating test doubles.

Ease of Use

  • chai-spies:

    Chai Spies is easy to use and requires minimal setup. Its API is straightforward, making it accessible for developers who are new to testing.

  • jest:

    Jest is known for its user-friendly experience, with a simple setup process and clear documentation. Its built-in features reduce the need for additional configuration.

  • proxyquire:

    Proxyquire has a simple API for overriding dependencies, but may require some understanding of module loading in Node.js, which could be a slight hurdle for beginners.

  • rewire:

    Rewire is easy to use for those familiar with JavaScript modules, but accessing private variables may require additional understanding of the module's structure.

  • sinon:

    Sinon has a steeper learning curve due to its extensive features, but once mastered, it provides powerful capabilities for testing.

  • testdouble:

    Testdouble is designed to be minimal and intuitive, making it easy to create test doubles without unnecessary complexity.

Community and Support

  • chai-spies:

    Chai Spies has a supportive community and is widely used alongside Chai, which has extensive documentation and resources available.

  • jest:

    Jest has a large and active community, with extensive documentation, tutorials, and resources available for developers. It is widely adopted in the React ecosystem.

  • proxyquire:

    Proxyquire has a smaller community compared to Jest, but it is well-documented and has enough resources to assist users in its implementation.

  • rewire:

    Rewire has a decent community, but its documentation is less comprehensive than some of the larger libraries. Users may need to rely on community forums for support.

  • sinon:

    Sinon has a strong community and is well-documented, making it easy to find support and resources for testing needs.

  • testdouble:

    Testdouble has a smaller community, but it is gaining traction due to its simplicity and clean API. Documentation is available, but may not be as extensive as larger libraries.

Performance

  • chai-spies:

    Chai Spies is lightweight and does not introduce significant overhead, making it suitable for performance-sensitive tests.

  • jest:

    Jest is optimized for performance, with features like parallel test execution and intelligent test running that only runs tests affected by changes, improving overall speed.

  • proxyquire:

    Proxyquire can introduce some performance overhead due to the module overriding process, but it is generally efficient for most use cases.

  • rewire:

    Rewire is efficient but may add some overhead when accessing private variables, especially in complex modules.

  • sinon:

    Sinon is designed for performance, but extensive use of spies and mocks can lead to increased memory usage if not managed properly.

  • testdouble:

    Testdouble is lightweight and focuses on performance, ensuring that creating test doubles does not significantly impact test execution time.

How to Choose: chai-spies vs jest vs proxyquire vs rewire vs sinon vs testdouble

  • chai-spies:

    Choose Chai Spies if you need a simple and lightweight solution for spying on function calls and tracking their behavior without the overhead of a full testing framework. It integrates well with other assertion libraries like Chai.

  • jest:

    Choose Jest if you want a comprehensive testing framework that includes built-in mocking, spying, and assertion capabilities. Jest is particularly well-suited for React applications and offers a user-friendly experience with features like snapshot testing and parallel test execution.

  • proxyquire:

    Choose Proxyquire if you need to override module dependencies in your tests without modifying the original code. It allows you to replace dependencies with mocks or stubs, making it ideal for testing modules in isolation.

  • rewire:

    Choose Rewire if you want to modify the behavior of modules during testing without changing the actual source code. It allows you to access and modify private variables and functions, making it useful for testing internal logic.

  • sinon:

    Choose Sinon if you require a powerful library for creating spies, mocks, and stubs. It provides extensive capabilities for tracking function calls and controlling behavior, making it suitable for complex testing scenarios.

  • testdouble:

    Choose Testdouble if you prefer a minimalistic approach to mocking and stubbing with a focus on simplicity and readability. It provides a clean API for creating test doubles and is designed to work seamlessly with other testing frameworks.

README for chai-spies

Chai Spies

This is an addon plugin for the chai assertion library. It provides the most basic function spy ability and tests.

This library is primarily meant to serve as a starting point for anyone interested in developing chai plugins. If developing a module, you are welcome to use this as a starting point. I also encourage the use of the compile tools to allow modules to work both in node.js and the browser.

Installation

Node.js

Chai spies are available on npm.

  $ npm install chai-spies

Browser

Include chai-spies.js after including chai.js.

<script src="chai-spies.js"></script>

Plug In

If you are using chai-spies in the browser, there is nothing you need to do. It will detect chai in the global namespace and automatically get used.

If you are using node, here is a useful bit.

const chai = require('chai')
  , spies = require('chai-spies');

chai.use(spies);

const should = chai.should()
  , expect = chai.expect;

TypeScript Setup

If using TypeScript, this is how you can import chai-spies.

import { expect } from 'chai';
import spies from 'chai-spies';
chai.use(spies);

Building for the Browser

Currently this package uses rollup to bundle source code. Just use npm run build to build browser version.

Chai Spies Api Reference

Creating Spies

In this module, a spy is either an empty function, or a wrapped named function. Once chai has been extended, you can create a spy through chai's own interface.

function original () {
  // do something cool
}

const spy = chai.spy(original);

// then use in place of original
ee.on('some event', spy);

// or use without original
const spyAgain = chai.spy();
ee.on('some other event', spyAgain);

spy.on

spy.on allows to add spy on existing method of an object

const array = [1, 2, 3];

chai.spy.on(array, 'push');

// or multiple spies
chai.spy.on(array, ['push', 'pop']);

It's also possible to provide custom implementation of spied method:

chai.spy.on(array, 'push', function (...items) {
  // custom implementation of `push` method
});

Using arrow functions, it's also easy to replace method implementation with constant:

chai.spy.on(array, 'push', () => 5);

// or more readable :)
chai.spy.on(array, 'push', returns => 5);

spy.interface

This method creates a mock (or spy object): an interface with spies on each of the object's methods. The object's methods have either fake implementations or no implementation.

// with no implementation
const arrayLike = chai.spy.interface('arrayLike', ['push', 'pop', 'filter']);

// with fake implementation
const arrayLike = chai.spy.interface({
  push(item) {
    this.__items = this.__items || [];
    return this.__items.push(item)
  },
  // other method implementations
});

arrayLike.push(5);

spy.returns (Deprecated)

chai.spy.returns is just a simple helper which creates a function that returns constant:

const returnTrue = chai.spy.returns(true);

returnTrue(); // true

Better to use arrow function:

const returnTrue = chai.spy(returns => true);

Sandboxes

Sandbox is a set of spies. Sandbox allows to track methods on objects and restore original methods with on restore call. To create sandbox:

const sandbox = chai.spy.sandbox();

describe('Array', () => {
  let array;

  beforeEach(() => {
    array = [];
    sandbox.on(array, ['push', 'pop']);
  });

  afterEach(() => {
    sandbox.restore(); // restores original methods on `array`
  })

  it('allows to add items', () => {
    array.push(1);

    expect(array.push).to.have.been.called.with(1);
  });
});

chai.spy.on and chai.spy.restore are bound to default sandbox. So to restore all methods spied by chai.spy.on, just call chai.spy.restore() (without arguments).

restore method accepts 2 optional arguments: object to restore and method or methods to restore. So, this calls are also valid:

const array = [1, 2, 3];

chai.spy.on(array, ['push', 'pop']);

chai.spy.restore(array) // restores all methods on object
chai.spy.restore(array, 'push') // restores only `push` method

Assertions

.spy

Asserts that object is a spy.

expect(spy).to.be.spy;
spy.should.be.spy;

.called

Assert that a spy has been called. Negation passes through.

expect(spy).to.have.been.called();
spy.should.have.been.called();

Note that called can be used as a chainable method.

.with

Assert that a spy has been called with a given argument at least once, even if more arguments were provided.

spy('foo');
expect(spy).to.have.been.called.with('foo');
spy.should.have.been.called.with('foo');

Will also pass for spy('foo', 'bar') and spy(); spy('foo').

If used with multiple arguments, assert that a spy has been called with all the given arguments at least once.

spy('foo', 'bar', 1);
expect(spy).to.have.been.called.with('bar', 'foo');
spy.should.have.been.called.with('bar', 'foo');

.with.exactly

Similar to .with, but will pass only if the list of arguments is exactly the same as the one provided.

spy();
spy('foo', 'bar');
expect(spy).to.have.been.called.with.exactly('foo', 'bar');
spy.should.have.been.called.with.exactly('foo', 'bar');

Will not pass for spy('foo'), spy('bar'), spy('bar'); spy('foo'), spy('foo'); spy('bar'), spy('bar', 'foo') or spy('foo', 'bar', 1).

Can be used for calls with a single argument too.

.always.with

Assert that every time the spy has been called the argument list contained the given arguments.

spy('foo');
spy('foo', 'bar');
spy(1, 2, 'foo');
expect(spy).to.have.been.called.always.with('foo');
spy.should.have.been.called.always.with('foo');

.always.with.exactly

Assert that the spy has never been called with a different list of arguments than the one provided.

spy('foo');
spy('foo');
expect(spy).to.have.been.called.always.with.exactly('foo');
spy.should.have.been.called.always.with.exactly('foo');

.nth(n).called.with

Asserts that the nth call of the spy has been made with the list of arguments provided. This assertion comes with other three flavors:

  • .first.called.with
  • .second.called.with
  • .third.called.with
spy('foo');
spy('bar');
spy('baz');
spy('foobar');
expect(spy).to.have.been.first.called.with('foo');
spy.should.have.been.first.called.with('foo');
expect(spy).on.nth(5).be.called.with('foobar');
spy.should.on.nth(5).be.called.with('foobar');

These assertions requires the spy to be called at least the number of times required, for example

spy('foo');
spy('bar');
expect(spy).to.have.been.third.called.with('baz');
spy.should.have.been.third.called.with('baz');

Won't pass because the spy has not been called a third time.

.once

Assert that a spy has been called exactly once.

expect(spy).to.have.been.called.once;
expect(spy).to.not.have.been.called.once;
spy.should.have.been.called.once;
spy.should.not.have.been.called.once;

.twice

Assert that a spy has been called exactly twice.

expect(spy).to.have.been.called.twice;
expect(spy).to.not.have.been.called.twice;
spy.should.have.been.called.twice;
spy.should.not.have.been.called.twice;

.exactly(n)

Assert that a spy has been called exactly n times.

expect(spy).to.have.been.called.exactly(3);
expect(spy).to.not.have.been.called.exactly(3);
spy.should.have.been.called.exactly(3);
spy.should.not.have.been.called.exactly(3);

.min(n) / .at.least(n)

Assert that a spy has been called minimum of n times.

expect(spy).to.have.been.called.min(3);
expect(spy).to.not.have.been.called.at.least(3);
spy.should.have.been.called.at.least(3);
spy.should.not.have.been.called.min(3);

.max(n) / .at.most(n)

Assert that a spy has been called maximum of n times.

expect(spy).to.have.been.called.max(3);
expect(spy).to.not.have.been.called.at.most(3);
spy.should.have.been.called.at.most(3);
spy.should.not.have.been.called.max(3);

.above(n) / .gt(n)

Assert that a spy has been called more than n times.

expect(spy).to.have.been.called.above(3);
expect(spy).to.not.have.been.called.gt(3);
spy.should.have.been.called.gt(3);
spy.should.not.have.been.called.above(3);

.below(n) / .lt(n)

Assert that a spy has been called fewer than n times.

expect(spy).to.have.been.called.below(3);
expect(spy).to.not.have.been.called.lt(3);
spy.should.have.been.called.lt(3);
spy.should.not.have.been.called.below(3);

Tests

Tests are written using mocha in the BDD interface. Node tests can be executed using npm test. Browser tests can be seen by opening test/browser/index.html.

Contributors

project : chai-spies
repo age : 3 years, 2 months
active : 26 days
commits : 77
files : 12
authors :
48 Jake Luer 62.3%
7 Glenn Jorde 9.1%
4 Keith Cirkel 5.2%
3 = 3.9%
3 Sergiy Stotskiy 3.9%
2 JamesMaroney 2.6%
2 PG Herveou 2.6%
2 Ryckes 2.6%
1 Veselin Todorov 1.3%
1 Steffen 1.3%
1 Daniel Walker 1.3%
1 Domenic Denicola 1.3%
1 Andre Jaenisch 1.3%
1 PG 1.3%

License

(The MIT License)

Copyright (c) 2012 Jake Luer jake@alogicalparadox.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.