@cloudcmd/stub
TypeScript icon, indicating that this package has built-in type declarations

4.0.1 • Public • Published

Stub NPM version Build Status Coverage Status

Simplest sinon.stub() alternative. With support of showing diff on calleddWith.

Install

npm i @cloudcmd/stub

API

stub([impl])

  • 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

stub().returns([value])

const fn = stub().returns('hello');
fn();
// returns
'hello';

stub().throws([error])

const fn = stub().throws(Error('hello'));
fn();
// throws
Error('hello');

stub().rejects([error])

const fn = stub().rejects(Error('hello'));
await fn();
// rejects
Error('hello');

stub().resolves([values])

const fn = stub().resolves('hello');
await fn();
// resolves
'hello';

stub().calledWith([args])

const fn = stub();

fn('hello', 'world');

fn.calledWith('hello', 'world');
// returns true

stub().calledWithNew()

const fn = stub();

new fn();

fn.calledWithNew();
// returns
true;

stub().calledBefore(fn)

const fn1 = stub();
const fn2 = stub();

fn1();
fn2();

fn1.calledBefore(fn2);
// returns
true;

stub().calledAfter(fn)

const fn1 = stub();
const fn2 = stub();

fn1();
fn2();

fn2.calledAfter(fn1);
// returns
true;

stub().called

const fn = stub();

fn.called;
// returns
false;

fn();

fn.called;
// returns
true;

stub().callCount

const fn = stub();

fn.callCount;
// returns
0;

fn();

fn.callCount;
// returns
1;

stub().args

const fn = stub();

fn.args;
// returns
[];

fn(1);

fn.args;
// returns
[[1]];

stub().callId

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;

isStub(fn)

Check if provided function is stub.

const {stub, isStub} = require('@cloudcmd/stub');
const fn = stub();

isStub(fn);
// returns
true;

isStub(() => {});
// returns
false;

Related

License

MIT

Package Sidebar

Install

npm i @cloudcmd/stub

Weekly Downloads

167

Version

4.0.1

License

MIT

Unpacked Size

11.8 kB

Total Files

7

Last publish

Collaborators

  • coderaiser