
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Simple spy and stub functions for testing purposes.
You need to mock or spy a function and sinon.js feels like an overkill? Here are 2 stupidly simple helpers.
$ npm install spy-stub
const { spy } = require('spy-stub');
const object = {
snore(volume) {
return `Snore with volume ${volume}`;
};
};
it('should spy on a "snore" function', () => {
const snoreSpy = spy(object, 'snore');
const result = object.snore(5);
assert.equal(snoreSpy.called, 1, 'Snore called once');
const firstCallArgs = snoreSpy.args[0]; // "args" is list of arguments for each call
const firstArgument = firstCallArgs[0];
assert.equal(firstArgument, 5, 'Snore called with volume 5');
assert.equal(result, 'Snore with volume 5', 'Result is correct');
});
const { stub } = require('spy-stub');
function functionToTest(number, snore) {
if (number === 5) {
return snore(number);
}
};
it('should stub "snore" function', () => {
const snoreStub = stub();
functionToTest(5, snoreStub);
assert.equal(snoreStub.called, 1, 'Snore called once');
const firstCallArgs = snoreSpy.args[0];
const firstArgument = firstCallArgs[0];
assert.equal(firstArgument, 5, 'Snore called with volume 5');
});
const { stub } = require('spy-stub');
function functionToTest(number, snore) {
return snore(number);
};
it('should stub "snore" function', () => {
const snoreStub = stub(() => 'mock data');
const result = functionToTest(5, snoreStub);
assert.equal(snoreStub.called, 1, 'Snore called once');
const firstCallArgs = snoreSpy.args[0];
const firstArgument = firstCallArgs[0];
assert.equal(firstArgument, 5, 'Snore called with volume 5');
assert.equal(result, 'mock date', 'Result is mocked'); // <-- result is mocked
});
const { stub } = require('spy-stub');
it('should stub "snore" function', () => {
const object = {
snore(volume) {
return `Snore with volume ${volume}`;
};
};
const snoreStub = stub(object, 'snore', () => 'mock data');
const result = object.snore(5);
assert.equal(snoreStub.called, 1, 'Snore called once');
const firstCallArgs = snoreSpy.args[0];
const firstArgument = firstCallArgs[0];
assert.equal(firstArgument, 5, 'Snore called with volume 5');
assert.equal(result, 'mock date', 'Result is mocked'); // <-- result is mocked
});
FAQs
Spy and stub utils for testing
We found that spy-stub 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.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.