Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

simple-mock

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-mock

Super simple mocks, stubs, and spies with 1-step sandbox restore

  • 0.2.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
12K
decreased by-14.27%
Maintainers
1
Weekly downloads
 
Created
Source

simple-mock Project status

Super simple mocks, stubs, and spies with 1-step sandbox restore.

Install

npm install simple-mock

Mock

You can mock any function or value on an object and easily restore it.

Examples

simple.mock(obj, 'example', 'value'); // Replace with this value
simple.mock(obj, 'example', function() {}); // Replace with this function

simple.mock(obj, 'example') // Spy on underlying method *or* stub
simple.mock(obj, 'example').callbackWith(null, 'etc'); // Stub
simple.mock(obj, 'example').returnWith('etc'); // Stub
simple.mock(obj, 'example').throwWith(new Error()); // Stub

Then, to make sure all objects are back to the state the were in before your mocks:

simple.restore(); // Ideally called in an afterEach() block

callbackWith, returnWith and throwWith can be chained for queued behaviour, e.g.

simple.mock(Something.prototype, 'example')
  .callbackWith(null, 'etc')
  .callbackWith(new Error());

callbackWith, returnWith and throwWith configurations are stored on a simple array fn.actions

Expectations

You define your expectations with your own choice of assertion library.

assert(fn.called);
assert.equals(fn.callCount, 3);
assert.equals(fn.lastCall.args[0], error); // First parameter of the last call
assert.equals(fn.calls[0].returned, 'etc');
assert.equals(fn.calls[1].threw, error);

Standalone Stubs and Spies

If you need to create a standalone stub (stubs are also spies):

simple.stub().callbackWith(null, 'etc');
simple.stub().returnWith('etc');
simple.stub().throwWith(new Error());

Or spy on a standalone function:

var fn = simple.spy(function(){});

assert.equals(fn.callCount, 0);
assert.equals(fn.calls, []);

Keywords

FAQs

Package last updated on 03 Nov 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc