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

Fix: listen to 'start' and 'end' events when using Mocha programmatically #3660

Merged
merged 1 commit into from
Mar 9, 2020

Conversation

juergba
Copy link
Contributor

@juergba juergba commented Jan 6, 2019

Description

mocha.run() returns a runner instance. Since the EVENT_RUN_BEGIN (start) event is emitted synchronously, it's impossible to register an EVENT_RUN_BEGIN listener in time. The event is emitted, but never catched.

When no tests are provided - eg. by a too restrictive grep expression - then neither EVENT_RUN_BEGIN nor EVENT_RUN_END (end) events can be listened to.

var mocha = new Mocha();

mocha.addFile("../eventStart.js");

mocha.run()
  .on(EVENT_RUN_BEGIN, () => console.log('start'))    // never runs
  .on(EVENT_RUN_END, () => console.log('end'));       // never runs when no tests are provided

Description of Change

We delay the start of the root Suite by Runner.immediately. The order of the event emissions remains unchanged.

Applicable issues

#3395 (backed out)
#3331
#3065
closes #2753

@coveralls
Copy link

coveralls commented Jan 6, 2019

Coverage Status

Coverage increased (+0.07%) to 92.97% when pulling 1f31f4e on juergba:juergba/issue/3414 into 9cbb6f6 on mochajs:master.

@juergba
Copy link
Contributor Author

juergba commented Jan 6, 2019

I messed up my branch, now everything is ok. Hopefully this will reopen my PR.

@juergba juergba reopened this Jan 6, 2019
@plroebuck
Copy link
Contributor

There is no way to register EventListener to the runner instance when Mocha runs programmatically with mocha.run().

Isn't this already possible with delayed root suite?

@juergba
Copy link
Contributor Author

juergba commented Jan 8, 2019

@plroebuck
I don't expect the user to know the answer, if I (we) don't know it without analyzing code and try-and-error testing. It wouldn't be an user-friendly solution.

yes, but... : if there are test files found, which include the (delay) run() function. (no: waiting)
no: if there are no test files found (grep)
no: waiting event doesn't work at all, which is emitted before start event

--delay does alter the event firing. start and end events are emitted twice, probably one additional set while the files get loaded.

@juergba juergba changed the title WIP: EventListener for runner instance when running programmatically with mocha.run() EventListener for runner instance when running programmatically with mocha.run() Jan 8, 2019
@outsideris outsideris added the area: usability concerning user experience or interface label Jan 26, 2019
Copy link
Contributor

@boneskull boneskull left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems we could instantiate the Runner in the Mocha constructor instead of in Mocha#run(), and then make it a property of the Mocha instance:

function Mocha() {
  // create root suite, process options
  this.runner = new Runner(rootSuite, options.delay);
}

Then users could:

const mocha = new Mocha();
mocha.runner.on('stuff', () => {})

Needs conflicts resolved

@boneskull boneskull added the semver-minor implementation requires increase of "minor" version number; "features" label Feb 27, 2019
@juergba juergba changed the title EventListener for runner instance when running programmatically with mocha.run() WIP: Registration of EventListener to the "runner" instance with mocha.run() Mar 14, 2019
@juergba juergba added status: needs review a maintainer should (re-)review this pull request and removed pr-needs-work labels Mar 14, 2019
@juergba juergba removed the status: needs review a maintainer should (re-)review this pull request label Mar 31, 2019
@juergba juergba self-assigned this Mar 31, 2019
@juergba juergba added area: async related to asynchronous use of Mocha type: bug a defect, confirmed by a maintainer semver-patch implementation requires increase of "patch" version number; "bug fixes" and removed semver-minor implementation requires increase of "minor" version number; "features" area: usability concerning user experience or interface labels Feb 28, 2020
@juergba juergba dismissed boneskull’s stale review February 28, 2020 08:21

Outdated, I started a new approach to fix this issue

@juergba juergba changed the title WIP: Registration of EventListener to the "runner" instance with mocha.run() Fix: listen to 'start' and 'end' events when using Mocha programmatically Feb 28, 2020
@juergba juergba requested review from a team and removed request for outsideris, craigtaub and plroebuck February 28, 2020 08:52
@juergba juergba added the status: needs review a maintainer should (re-)review this pull request label Feb 28, 2020
@mochajs mochajs deleted a comment from plroebuck Feb 28, 2020
@mochajs mochajs deleted a comment from boneskull Feb 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: async related to asynchronous use of Mocha landed-on-v7.1.x cherry-picked from master semver-patch implementation requires increase of "patch" version number; "bug fixes" type: bug a defect, confirmed by a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

unable to listen to start event during programmatic run
5 participants