Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/louisbrunner/sinon-spy-utils
A collection of util functions to make your life easier when using Sinon.js
https://github.com/louisbrunner/sinon-spy-utils
js npm sinon tdd utils
Last synced: about 1 month ago
JSON representation
A collection of util functions to make your life easier when using Sinon.js
- Host: GitHub
- URL: https://github.com/louisbrunner/sinon-spy-utils
- Owner: LouisBrunner
- License: mit
- Created: 2017-03-22T16:17:12.000Z (almost 8 years ago)
- Default Branch: main
- Last Pushed: 2023-10-18T07:47:09.000Z (over 1 year ago)
- Last Synced: 2024-04-14T19:43:14.624Z (10 months ago)
- Topics: js, npm, sinon, tdd, utils
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/sinon-spy-utils
- Size: 2.89 MB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sinon-spy-utils [![NPM Version][npm-image]][npm-url] ![Build Status][ci-image] [![Coverage Status][coveralls-image]][coveralls-url] [![dependencies Status][deps-image]][deps-url] [![devDependencies Status][deps-dev-image]][deps-dev-url]
This package is a collection of util functions to make your life easier when using [Sinon.js](http://sinonjs.org).
At the moment, it contains *3* functions: `Mock` (creating fake objects), `SpyAndDo` (scoped spy) and `StubAndDo` (scoped stub).## Installation
### Node Installation
```sh
npm install sinon-spy-utils
```You can then `SpyUtils = require('sinon-spy-utils')` or `import { Mock } from 'sinon-spy-utils'`.
### Browser Installation
Use the minified UMD build in the `dist` folder: [here](dist/sinon-spy-utils.min.js).
It exports a global `window.SinonSpyUtils` when imported as a `` tag.## Usage
Every code snippet will be presented in 3 different styles: Node.js `require`, Node.js `import` and Browser Javascript (with required HTML `<script>`s).
### Mock
This function creates a mock object containing functions corresponding to the provided names.
- *require*:
```js
var Mock = require('sinon-spy-utils').Mock;
...
var fakeUser = Mock('getName', 'getAvatar', 'getAge');
```- *import*:
```js
import { Mock } from 'sinon-spy-utils';
...
const fakeUser = Mock('getName', 'getAvatar', 'getAge');
```- *browser*:
```js
<script src="sinon.min.js">
...
var fakeUser = SinonSpyUtils.Mock('getName', 'getAvatar', 'getAge');
```### SpyAndDo
This function will spy the listed functions of the provided object and call the provided function. The spied functions will be restored whatever happens.
- *require*:
```js
var SpyAndDo = require('sinon-spy-utils').SpyAndDo;
...
SpyAndDo(console, 'error', 'log', function (spies) {
functionThatCallsConsoleLog(); // your function
expect(spies.log).to.have.been.called; // your test (here using sinon + chai style)
});
```- *import*:
```js
import { SpyAndDo } from 'sinon-spy-utils';
...
SpyAndDo(console, 'error', 'log', (spies) => {
functionThatCallsConsoleLog(); // your function
expect(spies.log).to.have.been.called; // your test (here using sinon + chai style)
});
```- *browser*:
```js
...
SinonSpyUtils.SpyAndDo(console, 'error', 'log', function (spies) {
functionThatCallsConsoleLog(); // your function
expect(spies.log).to.have.been.called; // your test (here using sinon + chai style)
});
```### StubAndDo
This function will stub the listed functions of the provided object and call the provided function. The stubbed functions will be restored whatever happens.
- *require*:
```js
var StubAndDo = require('sinon-spy-utils').StubAndDo;
...
StubAndDo(console, 'error', 'log', function (spies) {
functionThatCallsConsoleLog(); // your function
expect(spies.log).to.have.been.called; // your test (here using sinon + chai style)
});
```- *import*:
```js
import { StubAndDo } from 'sinon-spy-utils';
...
StubAndDo(console, 'error', 'log', (spies) => {
functionThatCallsConsoleLog(); // your function
expect(spies.log).to.have.been.called; // your test (here using sinon + chai style)
});
```- *browser*:
```js
...
SinonSpyUtils.StubAndDo(console, 'error', 'log', function (spies) {
functionThatCallsConsoleLog(); // your function
expect(spies.log).to.have.been.called; // your test (here using sinon + chai style)
});
```## License
MIT, Copyright (c) 2017-2020 Louis Brunner
[npm-image]: https://img.shields.io/npm/v/sinon-spy-utils.svg
[npm-url]: https://npmjs.org/package/sinon-spy-utils
[ci-image]: https://github.com/LouisBrunner/sinon-spy-utils/workflows/Build/badge.svg
[coveralls-image]: https://coveralls.io/repos/github/LouisBrunner/sinon-spy-utils/badge.svg?branch=master
[coveralls-url]: https://coveralls.io/github/LouisBrunner/sinon-spy-utils?branch=master
[deps-image]: https://david-dm.org/louisbrunner/sinon-spy-utils/status.svg
[deps-url]: https://david-dm.org/louisbrunner/sinon-spy-utils
[deps-dev-image]: https://david-dm.org/louisbrunner/sinon-spy-utils/dev-status.svg
[deps-dev-url]: https://david-dm.org/louisbrunner/sinon-spy-utils?type=dev