Skip to content

run global setup before test files load; closes #4508 #4511

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 1 commit into
base: main
Choose a base branch
from

Conversation

boneskull
Copy link
Member

BREAKING CHANGE

Mocha#run() was orchestrating if and when to run global setup/teardown fixtures, but Mocha#run() must be called after test files have been loaded. This is a problem, because:

  1. --delay may expect something created in a fixture to be accessible, but it won't be
  2. Any future support for async suites is similarly negatively impacted
  3. It was inconsistent between "watch" and "single run" mode; "watch" mode already has this behavior!

This change causes setup fixtures to run before any test files have been loaded. In Node.js, this happens in lib/cli/run-helpers.

  • Added two functions to Mocha; Mocha#globalSetupEnabled() and Mocha#globalTeardownEnabled() which both return booleans
  • Correct order of operations is asserted in test/integration/global-fixtures.spec.js
  • Removed low-value (and newly broken) unit tests where Mocha#run called runGlobalSetup/runGlobalTeardown
  • Add a note to browser-entry.js about global fixtures

This is breaking because:

  1. Browser users now must run setup/teardown fixtures manually.
  2. Somebody's test harness might expect test files to be loaded (and suites run) before global setup fixtures execute.

We should probably merge this, even though I'm loathe to break it--the original design will cause major headaches in future development. My bad!

Closes #4508

BREAKING CHANGE

`Mocha#run()` was orchestrating if and when to run global setup/teardown fixtures, but `Mocha#run()` must be called after test files have been loaded.  This is a problem, because:

1. `--delay` may expect something created in a fixture to be accessible, but it won't be
2. Any future support for async suites is similarly negatively impacted
3. It was inconsistent between "watch" and "single run" mode; "watch" mode already has this behavior!

This change causes setup fixtures to run _before_ any test files have been loaded.  In Node.js, this happens in `lib/cli/run-helpers`.

- Added two functions to `Mocha`; `Mocha#globalSetupEnabled()` and `Mocha#globalTeardownEnabled()` which both return booleans
- Correct order of operations is asserted in `test/integration/global-fixtures.spec.js`
- Removed low-value (and newly broken) unit tests where `Mocha#run` called `runGlobalSetup`/`runGlobalTeardown`
- Add a note to `browser-entry.js` about global fixtures

This is breaking because:

1. Browser users now must run setup/teardown fixtures manually.
2. Somebody's test harness might expect test files to be loaded (and suites run) _before_ global setup fixtures execute.
@boneskull boneskull added type: feature enhancement proposal area: usability concerning user experience or interface semver-major implementation requires increase of "major" version number; "breaking changes" labels Nov 13, 2020
@boneskull boneskull self-assigned this Nov 13, 2020
@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 94.138% when pulling 6bf7da7 on boneskull/issue/4508-global-fixtures into c6856ba on master.

@witrin
Copy link

witrin commented Nov 1, 2021

@boneskull Is there any chance of a merge in the near future? This issue is really a pain point for us.

@greguintow
Copy link

@juergba Could you take a look into this? It's still a pain

@mariobm
Copy link

mariobm commented Apr 26, 2022

What is the status of this one? It would be nice if it gets prioritized

@justin-is-a-builder
Copy link

Can you please commit this

@justin-is-a-builder
Copy link

@tj

@justin-is-a-builder
Copy link

@boneskull

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.

Looks good to me. Shoutout to 2020-era @boneskull for writing clear, maintainable code. 😄

Since this is a semver-major we'll definitely want reviews from the rest of @mochajs/maintenance-crew. Note that per #5027, even when we have approvals, we're still ramping up into bigger changes. This will take a while longer to get merged.

@JoshuaKGoldberg JoshuaKGoldberg added the status: blocked Waiting for something else to be resolved label Mar 4, 2024
await mocha.runGlobalTeardown(context);
}
done(...args);
});
Copy link
Member

Choose a reason for hiding this comment

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

Aside: https://github.com/mochajs/mocha/pull/4771/files#r1511173111 also talks about refactoring these shared wrappers. If I were to end up taking ownership over this PR then I'd probably apply that refactor. Food for thought.

@mdrobny
Copy link

mdrobny commented Mar 27, 2024

So glad to see this PR being resurrected 🎉 this bug is a pain in the ass 😄
I understand that maintenance-crew need to take a look at this and approve/improve but if there is something I could help with to push this forward then I can try ;)

@voxpelli voxpelli requested a review from Copilot June 12, 2025 07:46
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR changes Mocha’s execution flow so that global setup fixtures run before loading test files (both in serial and parallel CLI modes), introduces new public flags to query fixture enablement, and updates related integration tests and documentation.

  • Introduced Mocha#globalSetupEnabled() and Mocha#globalTeardownEnabled() methods
  • Moved fixture orchestration into lib/cli/run-helpers.js to run setup → load → tests → teardown
  • Updated integration tests to verify fixture ordering and added a sample fixture file

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
test/unit/mocha.spec.js Removed outdated unit tests around Mocha.run fixture behavior
test/integration/plugins/global-fixtures.spec.js Switched to new fixture path constant and tightened expectations to include the test fixture load order
test/integration/fixtures/plugins/global-fixtures/global.fixture.js Added a sample test fixture that logs loading and defines a passing suite
lib/plugin-loader.js Swapped Array.from for spread syntax when iterating export names
lib/mocha.js Removed built-in fixture calls from Mocha.run and added two new public enablement methods
lib/cli/run-helpers.js Updated single-run and parallel-run helpers to run global setup/teardown around file loading
browser-entry.js Noted that global fixtures must be run manually in the browser and linked APIs
Comments suppressed due to low confidence (2)

test/unit/mocha.spec.js:854

  • Add unit tests for the newly introduced Mocha#globalSetupEnabled() and Mocha#globalTeardownEnabled() methods to validate their behavior based on various enableGlobalSetup and enableGlobalTeardown option states.
describe('Mocha', function() {

lib/plugin-loader.js:225

  • [nitpick] Consider using Array.from(this.knownExportNames) instead of the spread operator for consistency with other parts of the codebase that use Array.from() to convert sets to arrays.
return [...this.knownExportNames].reduce(

* @public
* @returns {boolean}
*/
Mocha.prototype.globalTeardownEnabled = function globalSetupEnabled() {
Copy link
Preview

Copilot AI Jun 12, 2025

Choose a reason for hiding this comment

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

The function assigned to globalTeardownEnabled is incorrectly named globalSetupEnabled; this misnaming could lead to confusion in stack traces and debugging. Rename the function to globalTeardownEnabled.

Suggested change
Mocha.prototype.globalTeardownEnabled = function globalSetupEnabled() {
Mocha.prototype.globalTeardownEnabled = function globalTeardownEnabled() {

Copilot uses AI. Check for mistakes.

@mark-wiemer mark-wiemer self-requested a review June 13, 2025 00:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: usability concerning user experience or interface semver-major implementation requires increase of "major" version number; "breaking changes" status: blocked Waiting for something else to be resolved type: feature enhancement proposal
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🚀 Feature: Global fixtures should run before any files are loaded
8 participants