# sinon-mocha-test

> Automatic Sinon sandbox for Mocha tests

Latest version **3.0.1** (published 2024-10-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install sinon-mocha-test
pnpm add sinon-mocha-test
yarn add sinon-mocha-test
bun add sinon-mocha-test
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.1 |
| Published | 2024-10-01 |
| First published | 2020-06-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=18.18.0 |
| Dependencies | 1 |
| Unpacked size | 10.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | yes |
| Author | Peter West |
| Maintainers | peterjwest |
| Keywords | sinon, mocha, tests, test, unit, mock, stub, spy |

## Links

- npm: https://www.npmjs.com/package/sinon-mocha-test
- Repository: https://github.com/peterjwest/sinon-mocha-test
- Homepage: https://github.com/peterjwest/sinon-mocha-test#readme
- Issues: https://github.com/peterjwest/sinon-mocha-test/issues
- npm.io page: https://npm.io/package/sinon-mocha-test

## Dependencies (1)

- [sinon](https://npm.io/package/sinon.md) ^18.0.1

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads

## Recent versions

- 3.0.1 (latest) — 2024-10-01
- 3.0.0 — 2024-08-09
- 2.1.1 — 2023-08-17
- 2.1.0 — 2023-08-17
- 2.0.0 — 2023-04-11
- 1.2.0 — 2021-06-01
- 1.1.0 — 2020-08-15
- 1.0.8 — 2020-06-12
- 1.0.7 — 2020-06-12
- 1.0.6 — 2020-06-11
- 1.0.5 — 2020-06-11
- 1.0.4 — 2020-06-11
- 1.0.3 — 2020-06-11
- 1.0.2 — 2020-06-08
- 1.0.1 — 2020-06-08
- … 1 more at https://npm.io/package/sinon-mocha-test/versions

## README

# sinon-mocha-test [![npm version][npm-badge]][npm-url] [![build status][circle-badge]][circle-url] [![coverage status][coverage-badge]][coverage-url]

Automatic Sinon sandbox for Mocha/Jest/Vitest tests in Javascript and Typescript.

A utility function which wraps a test and automatically removes mocks.

## Installation

```bash
npm install sinon-mocha-test
```

## Usage

<!-- snippet: js-mocha,ts-jest -->
```js
import { promises as fs } from 'fs';
import assert from 'assert';
import sinonTest from 'sinon-mocha-test';

/** Example function to test */
async function readJsonFile(path) {
  return JSON.parse((await fs.readFile(path)).toString());
}

describe('readJsonFile', () => {
  it('Resolves with the data from a JSON file', sinonTest(async (sinon) => {
    const readFile = sinon.stub(fs, 'readFile').resolves('{"version":"123"}\n');
    assert.deepStrictEqual(await readJsonFile('file.json'), { version: '123' });
    assert.strictEqual(readFile.callCount, 1);
  }));
});
```

Or with Vitest:

<!-- snippet: ts-vite,js-vite -->
```js
import { test, describe } from 'vitest'
import { promises as fs } from 'fs';
import assert from 'assert';
import sinonTest from 'sinon-mocha-test';

/** Example function to test */
async function readJsonFile(path) {
  return JSON.parse((await fs.readFile(path)).toString());
}

describe('readJsonFile', () => {
  test('Resolves with the data from a JSON file', () => {
    assert.strictEqual(1, 1);
  });

  test('Resolves with the data from a JSON file', sinonTest(async (sinon) => {
    const readFile = sinon.stub(fs, 'readFile').resolves('{"version":"123"}\n');
    assert.deepStrictEqual(await readJsonFile('file.json'), { version: '123' });
    assert.strictEqual(readFile.callCount, 1);
  }));
});
```

### Custom sandbox options

Use `sinonTest.create` to specify custom Sinon sandbox options:

<!-- snippet: js-mocha,ts-jest -->
```js
import sinonTest from 'sinon-mocha-test';

/** Example function to test */
async function delay(time) {
  return new Promise((resolve) => {
    setTimeout(resolve, time);
  });
}

describe('delay', () => {
  it('Resolves after a delay', sinonTest.create({ useFakeTimers: false }, async (sinon) => {
    await delay(10);
  }));
});
```

### With CommonJS / require()

<!-- snippet: cjs-mocha,cjs-jest -->
```js
const assert = require('assert');
const sinonTest = require('sinon-mocha-test');

/** Example function to test */
function logger(message) {
  console.log(message);
}

describe('logger', () => {
  it('Resolves after a delay', sinonTest(function(sinon) {
    const log = sinon.stub(console, 'log');
    logger('Hello world');
    assert.strictEqual(log.callCount, 1);
    assert(log.calledWith('Hello world'));
  }));
});
```

[npm-badge]: https://badge.fury.io/js/sinon-mocha-test.svg
[npm-url]: https://www.npmjs.com/package/sinon-mocha-test

[circle-badge]: https://circleci.com/gh/peterjwest/sinon-mocha-test.svg?style=shield
[circle-url]: https://circleci.com/gh/peterjwest/sinon-mocha-test

[coverage-badge]: https://coveralls.io/repos/peterjwest/sinon-mocha-test/badge.svg?branch=main&service=github
[coverage-url]: https://coveralls.io/github/peterjwest/sinon-mocha-test?branch=main

---
_Source: https://npm.io/package/sinon-mocha-test · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
