Security News
Opengrep Emerges as Open Source Alternative Amid Semgrep Licensing Controversy
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Top up your Jasmine spies with better fake calls.
Motivated by the fact that using spies in Jasmine requires a lot of boilerplate to perform different actions depending on provided arguments.
Currently supports Jasmine 2 only. 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).isCalled.thenThrow(new Error('some error'));
when(spy).isCalledWith('something').thenReturn('a value');
when(spy).isCalledWith('something else').thenReturn('a different value');
###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');
Mix default handlers and specific handlers
when(spy).isCalled.thenReturn(1);
when(spy).isCalledWith('two').thenReturn(2);
spy(); // => 1
spy('bar'); // => 1
spy('two'); // => 2
Make a spy call a particular function, when called with a specific argument
when(spy).isCalledWith('bar').then(function(arg) {
// Do something with arg
});
Make a spy throw an error
when(spy).isCalledWith('baz').thenThrow(new Error());
Works with jasmine.any & jasmine.objectContaining
when(spy).isCalledWith(jasmine.any(String)).thenReturn("string!");
when(spy).isCalledWith(jasmine.objectContaining({
foo : "bar"
})).thenReturn("object!");
spy('abc'); // => string!
spy({ foo : "bar" }); // => object!
Multiple callbacks can be added and will be executed in order
when(spy).isCalled.thenReturn(1)
.thenReturn(2)
.thenReturn(3)
.thenThrow(new Error('eof'));
spy(); // => 1
spy(); // => 2
spy(); // => 3
spy(); // Throws error
###Contributing
Say When is an open source project, maintained by Push Technology. Issues and pull requests are welcomed.
Tests can be run by installing a dev dependencies with npm install
and then running npm test
###License
Licensed under the Apache 2.0 license. See LICENSE.txt.
FAQs
Better spy fake calls for Jasmine
The npm package saywhen receives a total of 20 weekly downloads. As such, saywhen popularity was classified as not popular.
We found that saywhen demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.