Skip to content

Runner end event can’t be caught if the grep option matches no tests #3065

@Mr0grog

Description

@Mr0grog

Prerequisites

  • Checked that your issue isn't already filed by cross referencing issues with the common mistake label
  • Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn't just a feature that actually isn't supported in the environment in question or a bug in your code.
  • 'Smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
  • Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with:
    node node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend avoiding the use of globally installed Mocha.

Description

When running Mocha programmatically or in a browser, mocha.run() returns the test runner instance and event handlers can be attached to it. However, when the grep option is used and it matches no tests, ]it emits the end event synchronously](https://github.com/mochajs/mocha/blob/master/lib/runner.js#L621-L623) before the runner is ever returned from mocha.run() and before any third-party code could attach a listener.

I know I can work around this by providing a callback to mocha.run(), but it still seems reasonable to expect that you’d always have an opportunity to attach an event listener before the end event fires.

Steps to Reproduce

If I have a test suite at /single-test.js that looks like:

describe('Test suite', function () {
  it('A single passing test', function() {
    // this passes
  });
});

And I run the following:

const Mocha = require('mocha');
const mocha = new Mocha({ui: 'bdd', grep: /unmatchable/});
mocha.addFile('/single-test.js');
mocha
  .run()
  .on('end', () => console.log('End event handled!'));

Expected behavior: I expect “End event handled!” to be logged to the console.

Actual behavior: The test suite runs, but my handler does not.

Reproduces how often: 100%

Versions

Mocha 4.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions