Skip to content

fix: hook exit code #5058

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

Open
wants to merge 6 commits into
base: 3.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/mocha/asyncWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,19 @@ module.exports.injected = function (fn, suite, hookName) {
const errHandler = err => {
recorder.session.start('teardown')
recorder.cleanAsyncErr()
if (hookName == 'before' || hookName == 'beforeSuite') suiteTestFailedHookError(suite, err, hookName)
if (hookName === 'after') suite.eachTest(test => event.emit(event.test.after, test))
if (hookName === 'afterSuite') event.emit(event.suite.after, suite)
if (hookName === 'before' || hookName === 'beforeSuite') {
Copy link
Preview

Copilot AI Aug 5, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider using a switch statement or array includes for better readability when checking multiple hook types, especially as this pattern is repeated throughout the function.

Suggested change
if (hookName === 'before' || hookName === 'beforeSuite') {
if (['before', 'beforeSuite'].includes(hookName)) {

Copilot uses AI. Check for mistakes.

suiteTestFailedHookError(suite, err, hookName)
}
if (hookName === 'after') {
suiteTestFailedHookError(suite, err, hookName)
suite.eachTest(test => {
event.emit(event.test.after, test)
})
}
if (hookName === 'afterSuite') {
suiteTestFailedHookError(suite, err, hookName)
event.emit(event.suite.after, suite)
}
recorder.add(() => doneFn(err))
}

Expand Down