Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kellyselden/mocha-helpers
Mocha convenience helpers
https://github.com/kellyselden/mocha-helpers
Last synced: 16 days ago
JSON representation
Mocha convenience helpers
- Host: GitHub
- URL: https://github.com/kellyselden/mocha-helpers
- Owner: kellyselden
- License: mit
- Created: 2019-01-30T10:24:52.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2024-12-02T17:12:10.000Z (about 2 months ago)
- Last Synced: 2025-01-08T07:44:37.770Z (18 days ago)
- Language: JavaScript
- Size: 393 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mocha-helpers
[![npm version](https://badge.fury.io/js/mocha-helpers.svg)](https://badge.fury.io/js/mocha-helpers)
Mocha convenience helpers
## Usage
Place this file somewhere in your test directory:
```js
// test/helpers/mocha.js
require('mocha-helpers')(module);
```Then use it via:
```js
// test/unit/my-file/my-function-test.js
const { describe, it } = require('../../helpers/mocha');
const { myFunction } = require('my-file');describe(function() {
it(myFunction, function() {
// stuff
});it.allowFail('skip on error', function() {
assert.ok(false);
});
});
```Prints:
```
Unit | My-File
✓ myFunction
```## Retry Hooks
Make hooks follow `--retries` logic. https://github.com/mochajs/mocha/issues/2127.
```js
// test/helpers/mocha.js
require('mocha-helpers')(module, {
retryHooks: true
});
```Then use it via:
```js
// test/my-test.js
const { describe, beforeEach } = require('./helpers/mocha');describe(function() {
let retries = 0;
beforeEach(function() {
if (retries++ < 1) {
throw new Error();
}
});it('works', function() {
// stuff
});
});
``````
mocha test/my-test.js --retries 1
```Prints:
```
My-Test
✓ works
```## Async Events
Add a way to `await` Mocha's synchronous events.
```js
const { events, registerAsyncEvents, unregisterAsyncEvents } = require('mocha-helpers');let runner;
async function retry(test, err) {
// do something async on retries...
}try {
await new Promise((resolve, reject) => {
try {
events.on(constants.EVENT_TEST_RETRY, retry);runner = mocha.run(resolve);
registerAsyncEvents(runner);
} catch (err) {
reject(err);
}
});
} finally {
events.off(constants.EVENT_TEST_RETRY, retry);if (runner) {
await unregisterAsyncEvents(runner);
}
}
```## Options
```js
require('mocha-helpers')(module, {
dirname: __dirname,
titleSeparator: ' | ',
titleize: true,
prefix: '',
retryHooks: false
});
```