-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Open
Labels
area: documentationanything involving docs or mochajs.organything involving docs or mochajs.orgarea: usabilityconcerning user experience or interfaceconcerning user experience or interfacesemver-majorimplementation requires increase of "major" version number; "breaking changes"implementation requires increase of "major" version number; "breaking changes"status: accepting prsMocha can use your help with this one!Mocha can use your help with this one!
Description
Prerequisites
- Checked that your issue hasn't already been filed by cross-referencing issues with the
faq
labelChecked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn't just a feature that actually isn't supported in the environment in question or a bug in your code.'Smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itselfEnsured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with:node node_modules/.bin/mocha --version
(Local) andmocha --version
(Global). We recommend that you not install Mocha globally.To pick up a draggable item, press the space bar. While dragging, use the arrow keys to move the item. Press space again to drop the item in its new position, or press escape to cancel.
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:
Work identically parallel mode, watch mode, and serial mode
Now, before Mocha loads and runs your tests, it will execute the above global setup fixture, starting a server for testing.
Steps to Reproduce
fixtures.js
exports.mochaGlobalSetup = function() {
console.log(`mochaGlobalSetup`);
}
test.js
console.log('Test file loaded')
it('runs', () => { })
Expected behavior:
Should run mochaGlobalSetup
before loading the test files in all cases.
Actual behavior: [What actually happens]
> npx mocha -r fixtures.js test.js
Test file loaded
mochaGlobalSetup
✓ runs
1 passing (3ms)
> npx mocha -r fixtures.js test.js --watch
mochaGlobalSetup
Test file loaded
✓ runs
1 passing (2ms)
ℹ [mocha] waiting for changes...
Reproduces how often: 100%
Versions
node --version
:Unknown command: mocha
node node_modules/.bin/mocha --version
:8.2.1
node --version
:v10.22.1
- Your operating system:
macOS Catalina 10.15.7 64bit
- Your shell (e.g., bash, zsh, PowerShell, cmd):
fish
witrin, Properko and mdrobny
Metadata
Metadata
Assignees
Labels
area: documentationanything involving docs or mochajs.organything involving docs or mochajs.orgarea: usabilityconcerning user experience or interfaceconcerning user experience or interfacesemver-majorimplementation requires increase of "major" version number; "breaking changes"implementation requires increase of "major" version number; "breaking changes"status: accepting prsMocha can use your help with this one!Mocha can use your help with this one!
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
boneskull commentedon Nov 13, 2020
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:
[-]Global fixtures loading tests before mochaGlobalSetup[/-][+]global fixtures should run before any files are loaded[/+]run global setup before test files load; closes #4508
boneskull commentedon Nov 13, 2020
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..
lucasrcosta commentedon Nov 13, 2020
@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.
SgtPooki commentedon Nov 25, 2020
I left a comment saying the setup and teardown functions weren't being called for me, they are.. I was doing
instead of
too used to typescript ¯_(ツ)_/¯. Hopefully leaving this comment helps someone else?
witrin commentedon Nov 1, 2021
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.
witrin commentedon Dec 17, 2021
@boneskull How is the planing about this?
9 remaining items