Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Add the Powers of spying to JavaScript tests
npm install --save-dev austin
var austin = require('austin');
var testSubject = {
testFunction: function () {
return 5;
},
anotherFunction: function () {
return 'hello';
}
};
// Inform Austin of the method to spy on...
austin.spy(testSubject, 'testFunction');
// Austin adds spy utilities...
testSubject.testFunction.callCount();
// => 0
// Austin allows executing the function...
testSubject.testFunction('green', 3, {x: ['seven']});
// => 5
// Austin tracks the number of times the spied function was called...
testSubject.testFunction.callCount();
// => 1
// And the parameters passed to each call of the spied function
testSubject.testFunction.calls[0];
// => ['green', 3, {x: ['seven']}]
// And knows if a Spied Function was called with params...
testSubject.testFunction.calledWith(['green', 3, {x: ['seven']}]);
// => true
testSubject.testFunction.calledWith(['blue']);
// => false
// And can intercept the return value of the Spied Function
testSubject.test.returns('a fake value');
testSubject.test();
// => 'a fake value'
// And can intercept the return value of the Spied Function with specific parameters
testSubject.test.withArgs(['austin']).returns('powers');
testSubject.test('austin');
// => 'powers'
testSubject.test.withArgs(['austin']).callCount();
// => 1
// And supports chaining withArgs and returns
testSubject.test.returns('Groovy!')
.withArgs(['austin']).returns('powers')
.withArgs(['oooo']).returns('behave');
testSubject.test();
// => 'Groovy!'
testSubject.test('austin');
// => 'powers'
testSubject.test('oooo');
// => 'behave'
testSubject.test('Dr. Evil');
// => 'Groovy!'
// And can chain initial spy call
austin.spy(testSubject, 'anotherFunction').returns('bye')
.withArgs(['live']).returns('dangerously');
testSubject.anotherFunction();
// => 'bye'
testSubject.anotherFunction('live');
// => 'dangerously'
// Austin can reset spy analytics and fake value returns...
testSubject.testFunction.withArgs('Dr.').returns('Evil');
testSubject.testFunction.reset();
testSubject.testFunction.callCount();
// => 0
testSubject.testFunction.calls;
// => []
testSubject.testFunction('Dr.');
// => 5
// When Austin is told to halt spying...
testSubject.testFunction.restore();
// Spy utilities are removed...
testSubject.testFunction.callCount;
// => undefined
// And the function returns to its former self.
testSubject.testFunction();
// => 5
// And Austin is smart enough to make new spies...
var nigelPowers = austin.spy();
nigelPowers.returns('Judo Chop');
nigelPowers();
// => 'Judo Chop'
Creates a new Spied Function.
Adds spy related utilities to obj[methodName] by transforming the function to a Spied Function.
Note: returns Spied Function for easy chaining with returns and withArgs.
Type: Object
An object that has the desired method methodName
to be spied on.
Type: *
Can be any type as long as obj[methodName]
is a function
.
A Spied Function has additional properties and methods to aid testing
Executes the original function, while updating spy analytics such as call count.
The number of times spiedFunction was executed.
Returns a Boolean
if spiedFunction was called with params
.
Type: Array
Params is an array of any types.
An array of parameters passed to each execution of Spied Function.
Resets spiedFunction.callCount() to 0, spiedFunction.calls to [], and resets any fake values set with returns.
Restores the spiedFunction to its original function and removes all spy utilities.
When spiedFunction is called, it will return value
instead of executing the original function.
Note: returns spiedFunction for easy chaining with withArgs.
Note: if calling spiedFunction.withArgs([...]).returns(...)
with params
that already have a fake value, then the
new value passed to returns
will override the previous value.
Type: *
Any value that is desired to be returned by spiedFunction().
Returns an object with a returns method to fake return values of calls to spiedFunction with params and callCount.
Note: spiedFunction.withArgs([...]).returns(...)
returns spiedFunction for easy chaining like spiedFunction.withArgs([...]).returns(...).withArgs([...]).returns(...)
.
Type: Array
An array of any types
MIT © Dustin Specker
FAQs
Add the Powers of spying to JavaScript tests
The npm package austin receives a total of 0 weekly downloads. As such, austin popularity was classified as not popular.
We found that austin 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
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.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.