# mocha-per-file

> This CLI tool allows to organize mocha tests in files and directories. It works by traversing the test directory recursively, creating describe blocks per directory and tests per *.test.js file.

Latest version **4.0.8** (published 2022-01-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install mocha-per-file
pnpm add mocha-per-file
yarn add mocha-per-file
bun add mocha-per-file
```

Provides the command `mocha-per-file`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.8 |
| Published | 2022-01-12 |
| First published | 2019-10-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=12 |
| Dependencies | 7 |
| Unpacked size | 12.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Sebastian Landwehr |
| Maintainers | dword-design |
| Keywords | chdir, cli, cwd, describe, directory, file, folder, it, manage, mocha, organize, recursive, test, testing, tree |

## Links

- npm: https://www.npmjs.com/package/mocha-per-file
- Repository: https://github.com/dword-design/mocha-per-file
- Homepage: https://github.com/dword-design/mocha-per-file#readme
- Issues: https://github.com/dword-design/mocha-per-file/issues
- Funding: https://github.com/sponsors/dword-design
- npm.io page: https://npm.io/package/mocha-per-file

## Dependencies (7)

- [execa](https://npm.io/package/execa.md) ^5.0.0
- [mocha](https://npm.io/package/mocha.md) ^9.0.0
- [globby](https://npm.io/package/globby.md) ^11.0.1
- [fs-extra](https://npm.io/package/fs-extra.md) ^10.0.0
- [make-cli](https://npm.io/package/make-cli.md) ^2.0.0
- [minimatch](https://npm.io/package/minimatch.md) ^3.0.4
- [@dword-design/chdir](https://npm.io/package/@dword-design/chdir.md) ^2.0.0

## 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
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 4.0.8 (latest) — 2022-01-12
- 4.0.7 — 2021-12-28
- 4.0.6 — 2021-07-20
- 4.0.5 — 2021-07-06
- 4.0.4 — 2021-06-14
- 4.0.3 — 2021-06-09
- 4.0.2 — 2021-05-28
- 4.0.1 — 2021-05-04
- 4.0.0 — 2021-05-03
- 3.0.8 — 2021-04-26
- 3.0.7 — 2021-04-12
- 3.0.6 — 2021-03-31
- 3.0.5 — 2021-03-23
- 3.0.4 — 2021-03-22
- 3.0.3 — 2021-03-17
- … 31 more at https://npm.io/package/mocha-per-file/versions

## README

<!-- TITLE/ -->
# mocha-per-file
<!-- /TITLE -->

<!-- BADGES/ -->
  <p>
    <a href="https://npmjs.org/package/mocha-per-file">
      <img
        src="https://img.shields.io/npm/v/mocha-per-file.svg"
        alt="npm version"
      >
    </a><img src="https://img.shields.io/badge/os-linux%20%7C%C2%A0macos%20%7C%C2%A0windows-blue" alt="Linux macOS Windows compatible"><a href="https://github.com/dword-design/mocha-per-file/actions">
      <img
        src="https://github.com/dword-design/mocha-per-file/workflows/build/badge.svg"
        alt="Build status"
      >
    </a><a href="https://codecov.io/gh/dword-design/mocha-per-file">
      <img
        src="https://codecov.io/gh/dword-design/mocha-per-file/branch/master/graph/badge.svg"
        alt="Coverage status"
      >
    </a><a href="https://david-dm.org/dword-design/mocha-per-file">
      <img src="https://img.shields.io/david/dword-design/mocha-per-file" alt="Dependency status">
    </a><img src="https://img.shields.io/badge/renovate-enabled-brightgreen" alt="Renovate enabled"><br/><a href="https://gitpod.io/#https://github.com/dword-design/mocha-per-file">
      <img
        src="https://gitpod.io/button/open-in-gitpod.svg"
        alt="Open in Gitpod"
        width="114"
      >
    </a><a href="https://www.buymeacoffee.com/dword">
      <img
        src="https://www.buymeacoffee.com/assets/img/guidelines/download-assets-sm-2.svg"
        alt="Buy Me a Coffee"
        width="114"
      >
    </a><a href="https://paypal.me/SebastianLandwehr">
      <img
        src="https://sebastianlandwehr.com/images/paypal.svg"
        alt="PayPal"
        width="163"
      >
    </a><a href="https://www.patreon.com/dworddesign">
      <img
        src="https://sebastianlandwehr.com/images/patreon.svg"
        alt="Patreon"
        width="163"
      >
    </a>
</p>
<!-- /BADGES -->

<!-- DESCRIPTION/ -->
This CLI tool allows to organize mocha tests in files and directories. It works by traversing the test directory recursively, creating describe blocks per directory and tests per *.test.js file.
<!-- /DESCRIPTION -->

<!-- INSTALL/ -->
## Install

```bash
# npm
$ npm install mocha-per-file

# Yarn
$ yarn add mocha-per-file
```
<!-- /INSTALL -->

## Usage

### Setup the directory structure

```bash
|- src
|- test
   |- api
   |  |- empty-input.test.js
   |  |- array.test.js
   |  |- exception.test.js
   |- cli
      |- args.test.js
      |- options.test.js
```

### Writing the tests

```js
// empty-input.test.js

const expect = require('expect')

// simple test
module.exports = () => expect(1).toEqual(1)

// test with timeout
module.exports = {
  it: expect(1).toEqual(2),
  timeout: 5000,
}

// test only this file
module.exports = {
  it: expect(1).toEqual(2),
  only: true,
}
```

### Calling mocha-per-file
In `npm test` in `package.json`:
```json
{
  "scripts": {
    "test": "mocha-per-file"
  }
}
```

Via bash:
```bash
$ npx mocha-per-file

api
  ✓ empty-input
  ✓ array
  ✓ exception

cli
  ✓ args
  ✓ options
```

### Specifying the root directory to search for tests:

```bash
$ npx mocha-per-file --path special-tests
```

### Passing parameters to mocha
Some mocha parameters are also supported and can be passed through. For information on this, check out `mocha-per-file --help`.

```bash
$ npx mocha-per-file --require @babel/register

```

If more are needed feel free to open an issue or a PR! 😃

### Execute only some tests
You can execute only some of the tests by providing a filename or glob. Please mind that the path has to be relative to the specified test folder.
```bash
$ npx mocha-per-file empty-input.test.js
$ npx mocha-per-file api/*
```

### Changing directories
It is possible to tell `mocha-per-file` to `chdir` into the directory of each test file when running the tests via the `--chdir` parameter:

```bash
$ npx mocha-per-file --chdir
```

This makes it much easier to work with local fixtures:

```js
// files.test.js

const expect = require('expect')
const { readFile } = require('fs-extra')

// process.cwd() is now in the test directory
module.exports = async () => expect(await readFile('foo.txt', 'utf8')).toEqual('foo')
```

### Using with-local-tmp-dir
[with-local-tmp-dir](https://www.npmjs.com/package/with-local-tmp-dir) is a package that allows us to create a temporary folder inside a given directory and remove it after having finished a callback. This makes it the perfect tool to run file-based tests. The following snippet illustrates this:

```js
// files.test.js

const expect = require('expect')
const { writeFile, readFile } = require('fs-extra')

module.exports = () => withLocalTmpDir(async () => {
  await writeFile('foo.txt', 'foo')
  expect(await readFile('foo.txt', 'utf8')).toEqual('foo')
})
```

<!-- LICENSE/ -->
## Contribute

Are you missing something or want to contribute? Feel free to file an [issue](https://github.com/dword-design/mocha-per-file/issues) or a [pull request](https://github.com/dword-design/mocha-per-file/pulls)! ⚙️

## Support

Hey, I am Sebastian Landwehr, a freelance web developer, and I love developing web apps and open source packages. If you want to support me so that I can keep packages up to date and build more helpful tools, you can donate here:

<p>
  <a href="https://www.buymeacoffee.com/dword">
    <img
      src="https://www.buymeacoffee.com/assets/img/guidelines/download-assets-sm-2.svg"
      alt="Buy Me a Coffee"
      width="114"
    >
  </a>&nbsp;If you want to send me a one time donation. The coffee is pretty good 😊.<br/>
  <a href="https://paypal.me/SebastianLandwehr">
    <img
      src="https://sebastianlandwehr.com/images/paypal.svg"
      alt="PayPal"
      width="163"
    >
  </a>&nbsp;Also for one time donations if you like PayPal.<br/>
  <a href="https://www.patreon.com/dworddesign">
    <img
      src="https://sebastianlandwehr.com/images/patreon.svg"
      alt="Patreon"
      width="163"
    >
  </a>&nbsp;Here you can support me regularly, which is great so I can steadily work on projects.
</p>

Thanks a lot for your support! ❤️

## License

[MIT License](https://opensource.org/licenses/MIT) © [Sebastian Landwehr](https://sebastianlandwehr.com)
<!-- /LICENSE -->

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