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

Adding a check in onHookEnd to only call error function if it exists and is a function (fixes #482) #484

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jamesmortensen
Copy link

@jamesmortensen jamesmortensen commented Aug 20, 2022

Context

Checklist

Fixes #482

The issue here is that, when running mocha in parallel mode with beforeEach or afterEach hooks, the onEndHook is called in MochaAllureReporter and the error() method is undefined.

Not sure about any unit tests for this. The project unit tests weren't passing before I made changes, so it's difficult to work with broken tests. Instead, I have a reproduceable example here: https://github.com/jamesmortensen/allure-js-reporter-issues#when-run-in-parallel-mode-with-hooks-hookerror-is-not-a-function

In the reproduceable example, run npx mocha -p test-with-hooks.js without the changes, to see the error occur, and run it with the changes to see the reporter function without errors and somewhat correctly. (By somewhat correctly, there is this related issue #485 where failed test cases in parallel mode are incorrectly reported as yellow/broken instead of red/failed.)

@github-actions github-actions bot added the theme:mocha Mocha related issue label Aug 20, 2022
@@ -81,6 +81,9 @@ export class MochaAllureReporter extends Mocha.reporters.Base {
}

private onHookEnd(hook: Mocha.Hook): void {
this.coreReporter.endHook(hook.error());
if(typeof(hook.error) === 'function')
this.coreReporter.endHook(hook.error());
Copy link
Member

Choose a reason for hiding this comment

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

Could we set default value for hook.error? Like just stub function to avoid additional conditions?
Or we can use optional chaining operator to reach the same result:

this.coreReporter.endHook(hook?.error?.());

Copy link
Author

Choose a reason for hiding this comment

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

@epszaw I will give it a try later in the month when I get some more time. Thanks for the feedback.

Copy link
Author

Choose a reason for hiding this comment

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

Hi @epszaw I just got back around to looking at this. It looks like we can close this pull request. I can see you added this change in this commit: cbb3426. Thanks for adding it in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme:mocha Mocha related issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Uncaught TypeError: hook.error is not a function MochaAllureReporter
2 participants