https://github.com/cloudcmd/stub
simplest sinon.stub() alternative with diff support
https://github.com/cloudcmd/stub
javascript nodejs sinon stub tdd test
Last synced: 18 days ago
JSON representation
simplest sinon.stub() alternative with diff support
- Host: GitHub
- URL: https://github.com/cloudcmd/stub
- Owner: cloudcmd
- License: mit
- Created: 2018-11-23T09:12:42.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-04-07T17:38:45.000Z (about 3 years ago)
- Last Synced: 2025-03-23T17:12:19.430Z (about 1 month ago)
- Topics: javascript, nodejs, sinon, stub, tdd, test
- Language: JavaScript
- Homepage:
- Size: 68.4 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: ChangeLog
- License: LICENSE
Awesome Lists containing this project
README
# Stub [![NPM version][NPMIMGURL]][NPMURL] [![Build Status][BuildStatusIMGURL]][BuildStatusURL] [![Coverage Status][CoverageIMGURL]][CoverageURL]
[NPMIMGURL]: https://img.shields.io/npm/v/@cloudcmd/stub.svg?style=flat&longCache=true
[BuildStatusURL]: https://github.com/coderaiser/putout/actions?query=workflow%3A%22Node+CI%22 "Build Status"
[BuildStatusIMGURL]: https://github.com/coderaiser/putout/workflows/Node%20CI/badge.svg
[NPMURL]: https://npmjs.org/package/@cloudcmd/stub "npm"
[CoverageURL]: https://coveralls.io/github/cloudcmd/stub?branch=master
[CoverageIMGURL]: https://coveralls.io/repos/cloudcmd/stub/badge.svg?branch=master&service=githubSimplest [sinon.stub()](https://sinonjs.org/) alternative. With support of showing diff on `calleddWith`.
## Install
```
npm i @cloudcmd/stub
```## API
### stub([impl])
- `impl` - stub implementation
```js
const stub = require('@cloudcmd/stub');
const fn = stub();
// fn contains stubbed functionconst asyncFn = stub(async () => {
throw Error('hi');
});// asyncFn contains stub async function
```### stub().returns([value])
```js
const fn = stub().returns('hello');
fn();
// returns
'hello';
```### stub().throws([error])
```js
const fn = stub().throws(Error('hello'));
fn();
// throws
Error('hello');
```### stub().rejects([error])
```js
const fn = stub().rejects(Error('hello'));
await fn();
// rejects
Error('hello');
```### stub().resolves([values])
```js
const fn = stub().resolves('hello');
await fn();
// resolves
'hello';
```### stub().calledWith([args])
```js
const fn = stub();fn('hello', 'world');
fn.calledWith('hello', 'world');
// returns true
```### stub().calledWithNew()
```js
const fn = stub();new fn();
fn.calledWithNew();
// returns
true;
```### stub().calledBefore(fn)
```js
const fn1 = stub();
const fn2 = stub();fn1();
fn2();fn1.calledBefore(fn2);
// returns
true;
```### stub().calledAfter(fn)
```js
const fn1 = stub();
const fn2 = stub();fn1();
fn2();fn2.calledAfter(fn1);
// returns
true;
```### stub().called
```js
const fn = stub();fn.called;
// returns
false;fn();
fn.called;
// returns
true;
```### stub().callCount
```js
const fn = stub();fn.callCount;
// returns
0;fn();
fn.callCount;
// returns
1;
```### stub().args
```js
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:
```js
const fn1 = stub();
const fn2 = stub();fn1();
fn2();fn1.callId;
// returns
1;fn2.callId;
// returns
2;
```### isStub(fn)
Check if provided function is stub.
```js
const {stub, isStub} = require('@cloudcmd/stub');
const fn = stub();isStub(fn);
// returns
true;isStub(() => {});
// returns
false;
```## Related
- [sinon-called-with-diff](https://github.com/coderaiser/sinon-called-with-diff) - sinon `calledWith` diff
- [try-to-tape](https://github.com/coderaiser/try-to-tape) - `try catch` for async tape tests
- [try-catch](https://github.com/coderaiser/try-catch "TryCatch") - functional try-catch wrapper.
- [try-to-catch](https://github.com/coderaiser/try-to-catch "TryToCatch") - functional try-catch wrapper for promises.## License
MIT