9.0.1 • Published 2 months ago

mocha-helpers v9.0.1

Weekly downloads
8,865
License
MIT
Repository
github
Last release
2 months ago

mocha-helpers

npm version

Mocha convenience helpers

Usage

Place this file somewhere in your test directory:

// test/helpers/mocha.js
require('mocha-helpers')(module);

Then use it via:

// 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.

// test/helpers/mocha.js
require('mocha-helpers')(module, {
  retryHooks: true
});

Then use it via:

// 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.

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

require('mocha-helpers')(module, {
  dirname: __dirname,
  titleSeparator: ' | ',
  titleize: true,
  prefix: '',
  retryHooks: false
});
9.0.1

2 months ago

9.0.0

6 months ago

8.0.0

10 months ago

6.3.0

1 year ago

7.1.0

1 year ago

6.2.4

1 year ago

6.2.3

1 year ago

6.2.2

1 year ago

7.0.0

1 year ago

7.0.1

1 year ago

6.1.0

2 years ago

6.2.1

2 years ago

6.2.0

2 years ago

6.0.1

3 years ago

6.0.0

3 years ago

5.1.1

3 years ago

5.1.0

4 years ago

5.0.0

4 years ago

4.8.0

4 years ago

4.7.0

4 years ago

4.6.1

4 years ago

4.6.0

4 years ago

4.5.3

4 years ago

4.5.2

4 years ago

4.5.1

4 years ago

4.5.0

4 years ago

4.4.1

4 years ago

4.4.0

4 years ago

4.3.2

4 years ago

4.3.1

4 years ago

4.3.3

4 years ago

4.3.0

4 years ago

4.2.1

5 years ago

4.2.0

5 years ago

4.1.0

5 years ago

4.0.0

5 years ago

3.4.0

5 years ago

3.3.1

5 years ago

3.3.0

5 years ago

3.2.2

5 years ago

3.2.1

5 years ago

3.2.0

5 years ago

3.1.0

5 years ago

3.0.0

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.4.0

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago

0.1.0

5 years ago

0.0.0

5 years ago