-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
🚀 Feature: Global fixtures should run before any files are loaded #4508
Comments
This is intended behavior, but
the reason it is the way it is: it'd otherwise be unusable in a browser without adding more API surface, and we try to keep things the same between node/browser as much as possible. this will be a problem if we allow async suites, since the suites would potentially try to use a resource (e.g. connect to a port) that a global setup fixture created... hm. two questions:
|
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.
I've created #4511 to address this. I agree that the behavior should change. Hopefully there hasn't been too much adoption of the feature yet.. |
@boneskull thank you for addressing this.
It does, since the test files are loaded before the fixtures. I'm working in a codebase that loads configurations from a database before importing the files to start a webserver. Some of it's dependencies use these configurations on top-level code (regardless of wether that's good practice or not), so today I cannot push global fixtures to the code since it would break the CI workflow.
No, in fact the behavior on watch mode is exactly that, so I've been developing that way the last few days. I understand I'll have to revert the introduction of global fixtures and wait for this to be finished in order to adopt it. By the way, this is a great feature and it makes the test setup process so much clearer. Specially if you can delay the files being loaded, so that all code can be in the proper place. Congratulations. |
I left a comment saying the setup and teardown functions weren't being called for me, they are.. I was doing exports = {
mochaGlobalSetup,
mochaGlobalTeardown,
mochaHooks,
}; instead of exports.mochaGlobalSetup = mochaGlobalSetup;
exports.mochaGlobalTeardown = mochaGlobalTeardown;
exports.mochaHooks = mochaHooks; too used to typescript ¯_(ツ)_/¯. Hopefully leaving this comment helps someone else? |
Without this patch we were currently not able to perform integration tests for one of our bigger systems in a clean and lean way. We really hope for a merge here. Because 9 is already released. |
@boneskull How is the planing about this? |
This feature would be a game-changer for me. It's really needed. |
what is the status on this? I need this fix in version 8.2.0 |
This issue is 2 years old, does anyone have a work-around? |
This prevents me from using global fixtures to set up integration tests. My use case is starting a database before importing a server module in the test file which requires the database to exist. I'll have to find another work around in the meantime. Appreciate all the hard work you folks put into Mocha! |
Prerequisites
faq
labelnode node_modules/.bin/mocha --version
(Local) andmocha --version
(Global). We recommend that you not install Mocha globally.Description
When using global fixtures, mocha loads the test files before running
mochaGlobalSetup
. This can cause issues when dependencies themselves load top-level configurations . But when--watch
is passed, it works as expected.This behavior differs from the following statements in the documentation:
Steps to Reproduce
fixtures.js
test.js
Expected behavior:
Should run
mochaGlobalSetup
before loading the test files in all cases.Actual behavior: [What actually happens]
Reproduces how often: 100%
Versions
node --version
:Unknown command: mocha
node node_modules/.bin/mocha --version
:8.2.1
node --version
:v10.22.1
macOS Catalina 10.15.7 64bit
fish
The text was updated successfully, but these errors were encountered: