Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@cloudcmd/stub

cloudcmd2.5kMIT4.0.1TypeScript support: included

simplest sinon.stub alternative with diff support

sinon, stub, async, promise, calledWith, diff, tape

readme

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;

License

MIT

changelog

2022.04.06, v4.0.1

fix:

  • (stub) homepage

2022.04.06, v4.0.0

feature:

  • (stub) drop support of node < 16
  • (package) madrun v9.0.2
  • (package) supertape v7.2.1
  • (package) putout v25.14.0
  • (package) eslint-plugin-putout v14.8.0
  • (package) eslint v8.12.0
  • (package) check-dts v0.6.6

2021.09.23, v3.7.1

fix:

  • (stub) types: withName

2021.09.23, v3.7.0

feature:

  • (stub) add calledBefore, calledAfter
  • (package) eslint-plugin-putout v10.3.0
  • (package) putout v20.4.0

2021.08.19, v3.6.0

feature:

  • (@cloudcmd/stub) travis -> github actions
  • (package) eslint v8.0.0-beta.0
  • (package) eslint-plugin-putout v9.2.1
  • (package) jest-diff v27.0.6
  • (package) putout v18.16.0
  • (package) supertape v6.0.2
  • (@putout/stub) add types

2021.02.19, v3.5.0

feature:

  • (package) putout v15.1.2
  • (@cloudcmd/stub) add resolves

2021.02.08, v3.4.0

feature:

  • (package) eslint-plugin-putout v7.1.1
  • (package) putout v14.1.3
  • (stub) add rejects
  • (package) supertape v4.0.0

2020.12.22, v3.3.0

feature:

  • (stub) add isStub

2020.12.22, v3.2.0

feature:

  • (stub) add watermark
  • (package) madrun v8.0.5
  • (package) putout v12.7.0
  • (package) supertape v3.9.1
  • (package) putout v11.10.0
  • (package) eslint-plugin-putout v6.7.0
  • (package) madrun v7.0.5

2020.05.13, v3.1.0

feature:

  • (package) eslint-plugin-putout v4.0.2
  • (stub) add throws
  • (package) try-to-catch v3.0.0
  • (package) try-catch v3.0.0
  • (package) supertape v2.0.0
  • (package) putout v8.2.0
  • (package) eslint v7.0.0
  • (package) madrun v6.0.1

2020.05.09, v3.0.0

feature:

  • (package) jest-diff v26.0.1
  • (stub) drop support of node < 10
  • (package) chalk v4.0.0

2020.02.07, v2.3.4

fix:

  • (@cloudcmd/stub) diff args

feature:

  • (package) nyc v15.0.0
  • (package) jest-diff v25.1.0
  • (package) eslint-plugin-node v11.0.0

2019.11.21, v2.3.3

feature:

  • (package) nodemon v2.0.0
  • (package) madrun v5.0.0
  • (package) eslint-plugin-node v10.0.0
  • (package) eslint-plugin-putout v3.0.0
  • (package) putout v7.1.0
  • (package) chalk v3.0.0
  • (package) strip-ansi v6.0.0
  • (package) try-to-catch v2.0.0

2019.08.26, v2.3.2

feature:

  • (stub) add madrun
  • (package) eslint v6.2.2
  • (package) eslint-plugin-node v9.1.0
  • (package) nyc v14.1.1
  • (package) jest-diff v24.9.0

2018.11.28, v2.3.1

fix:

  • (stub) anonymous

2018.11.28, v2.3.0

feature:

  • (stub) stub -> anonymous

2018.11.28, v2.2.0

feature:

  • (stub) add support of calledCount

2018.11.26, v2.1.2

fix:

  • (stub) F -> stub

2018.11.26, v2.1.1

feature:

  • (stub) F -> stub

2018.11.23, v2.1.0

feature:

  • (stub) add args

2018.11.23, v2.0.0

feature:

  • (stub) add calledWithNew