Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
4 tasks done
Mr0grog opened this issue Oct 11, 2017 · 3 comments
Closed
4 tasks done

Comments

@Mr0grog
Copy link

Mr0grog commented Oct 11, 2017

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

@ScottFreeCode
Copy link
Contributor

I wonder if fixing #2753 would automatically fix this too.

@Mr0grog
Copy link
Author

Mr0grog commented Oct 11, 2017

Ah! Yes, that definitely would. (Assuming end is guaranteed to come after start :P )

@boneskull
Copy link
Member

duplicate of #2753

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants