New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

saywhen

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

saywhen

Better spy fake calls for Jasmine

  • 0.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
29
decreased by-62.82%
Maintainers
1
Weekly downloads
 
Created
Source

saywhen

Provides conditional fake calls for Jasmine 2 spies.

Motivated by the fact that using spies in Jasmine requires a lot of boilerplate to perform different actions depending on provided arguments.

Heavily influenced by the Mockito library for Java.


Turn this...

spy.and.callFake = function(arg) {
    if (arg === 'something') {
		    return 'a value';
		} else if (arg === 'something else') {
		    return 'a different value';
		} else {
		    throw new Error('some error');
		}
};

... into this

when(spy).isCalledWith('something').thenReturn('a value');
when(spy).isCalledWith('something else').thenReturn('a value');
when(spy).isCalledWith(jasmine.any(Object)).thenThrow(new Error('some error'));

###Usage

Require Say When as a normal module

var when = require('saywhen');
var spy = jasmine.createSpy('foo');

Make a spy return a value when called with a specific argument

when(spy).isCalledWith('foo').thenReturn('bar');

Make a spy call a particular function, when called with a specific argument

when(spy).isCalledWith('bar').then(function(arg) {
    // Do something
});

Make a spy throw an error

when(spy).isCalledWith('baz').thenThrow(new Error());

Multiple callbacks can be added and will be executed in order

when(spy).isCalledWith().thenReturn(1)
                        .thenReturn(2)
                        .thenReturn(3)
                        .thenThrow(new Error('eof'));
                        
spy(); // => 1
spy(); // => 2
spy(); // => 3
spy(); // Throws error

###License

Licensed under the Apache 2.0 license. See LICENSE.txt.

Keywords

FAQs

Package last updated on 21 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