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: improve error handling in global fixtures #5208 #5275

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

TG199
Copy link
Contributor

@TG199 TG199 commented Dec 19, 2024

PR Checklist

Overview

Description
This PR addresses an issue with the handling of errors in global teardown functions. Previously, if a global teardown function threw an error, the error was not logged clearly, and the process exit code might not reflect the failure.

Changes Made
Updated the _runGlobalFixtures method in lib/mocha.js to:
Log errors with a clear and descriptive message.
Set the process exit code to 1 when a global teardown function fails.
Improved debugging output to aid in identifying which function caused the error.
New Behavior
When a global teardown function fails:
A descriptive error message is logged.
The process exits with a non-zero code, signaling the failure.

@TG199
Copy link
Contributor Author

TG199 commented Dec 19, 2024

All tests pass locally using npm run test-node:unit on both the main branch and my working branch. However, they fail in CI. This suggests an environment-specific issue. I'm investigating further and would appreciate any guidance on differences between the CI and local setups.

@JoshuaKGoldberg
Copy link
Member

All tests pass locally using npm run test-node:unit on both the main branch and my working branch. However, they fail in CI. This suggests an environment-specific issue. I'm investigating further and would appreciate any guidance on differences between the CI and local setups.

Ah, this looks like #5278. Sorry for the confusing errors here - you can ignore them.

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

A good start, thanks for sending this! I think there's some work to be done to flesh the PR out.

@@ -881,7 +881,7 @@ Mocha.prototype.failZero = function (failZero) {
* @return {Mocha} this
* @chainable
*/
Mocha.prototype.passOnFailingTestSuite = function(passOnFailingTestSuite) {
Mocha.prototype.passOnFailingTestSuite = function (passOnFailingTestSuite) {
Copy link
Member

Choose a reason for hiding this comment

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

This reminds me, I've been meaning to write a blog post about removing unrelated formatting changes from PRs... quick summary: unrelated changes clutter the Git history and make it harder to review PRs.

Suggested change
Mocha.prototype.passOnFailingTestSuite = function (passOnFailingTestSuite) {
Mocha.prototype.passOnFailingTestSuite = function(passOnFailingTestSuite) {

At some point we'll start enforcing formatting in this repo anyway.

try {
await fixtureFn.call(context);
} catch (err) {
console.error(err.stack);
Copy link
Member

Choose a reason for hiding this comment

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

[Functionality] This is a good start, but I think we'll want to do more than just log the error to the console. I think we'll want to mark the test as failed too. It's easy to miss console logs that don't change the exit code.

Copy link
Member

Choose a reason for hiding this comment

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

[Testing] This change is missing test coverage. It's great that existing tests pass, but there should be a test added that shows Mocha acting appropriately when an a global fixture does reject.

@JoshuaKGoldberg JoshuaKGoldberg added the status: waiting for author waiting on response from OP - more information needed label Jan 2, 2025
@TG199
Copy link
Contributor Author

TG199 commented Jan 2, 2025

Thanks for the review 🙏🏽. I'll make the necessary changes asap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting for author waiting on response from OP - more information needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 Bug: Error in mochaGlobalTeardown is swallowed and exits with code 0
2 participants