Simplest sinon.stub() alternative. With support of showing diff on calleddWith
.
npm i @cloudcmd/stub
impl
- stub implementation
const stub = require('@cloudcmd/stub');
const fn = stub();
// fn contains stubbed function
const asyncFn = stub(async () => {
throw Error('hi');
});
// asyncFn contains stub async function
const fn = stub().returns('hello');
fn();
// returns
'hello';
const fn = stub().throws(Error('hello'));
fn();
// throws
Error('hello');
const fn = stub().rejects(Error('hello'));
await fn();
// rejects
Error('hello');
const fn = stub().resolves('hello');
await fn();
// resolves
'hello';
const fn = stub();
fn('hello', 'world');
fn.calledWith('hello', 'world');
// returns true
const fn = stub();
new fn();
fn.calledWithNew();
// returns
true;
const fn1 = stub();
const fn2 = stub();
fn1();
fn2();
fn1.calledBefore(fn2);
// returns
true;
const fn1 = stub();
const fn2 = stub();
fn1();
fn2();
fn2.calledAfter(fn1);
// returns
true;
const fn = stub();
fn.called;
// returns
false;
fn();
fn.called;
// returns
true;
const fn = stub();
fn.callCount;
// returns
0;
fn();
fn.callCount;
// returns
1;
const fn = stub();
fn.args;
// returns
[];
fn(1);
fn.args;
// returns
[[1]];
Each stub
has it callId
, which can be used to determine order of stub
calls:
const fn1 = stub();
const fn2 = stub();
fn1();
fn2();
fn1.callId;
// returns
1;
fn2.callId;
// returns
2;
Check if provided function is stub.
const {stub, isStub} = require('@cloudcmd/stub');
const fn = stub();
isStub(fn);
// returns
true;
isStub(() => {});
// returns
false;
- sinon-called-with-diff - sinon
calledWith
diff - try-to-tape -
try catch
for async tape tests - try-catch - functional try-catch wrapper.
- try-to-catch - functional try-catch wrapper for promises.
MIT