Repository navigation
Add new --add-file CLI flag #3181
Description
Prerequisites
- Checked that your issue isn't already filed by cross referencing issues with the
common mistakelabel - Checked 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 itself
- Ensured 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 avoiding the use of globally installed Mocha.
Description
Currently there is a way for mocha to require a file globally via the CLI --require flag. This occurs before the mocha test suite is created and ran. This works and is great.
However there is no way to add a file to the mocha test suite via the CLI. There is a way programmatically via mocha.addFile() and you can simulate it via adding individual files when calling mocha mocha myAddFile1.js myAddFile2.js actualTestFile.js, but there is no official CLI way to always require files within the mocha test suite.
This is useful when setting up shared before and after handlers, or anything else that needs to act on the test suite.
I opened this ticket to explore the desirability of adding a --add-file CLI flag as an interface to the mocha.addFile function.
Additional Information
The desire for this addition came as a result of work I did when trying to integrate jest-runner-mocha into our tests suite.
Activity
Bamieh commentedon Dec 28, 2017
The CLI expects a list of files as arguments, which are added in order.
This will run the
hooks.jsfile then the*.test.jstests.Can you elaborate if this solves your issue? or if this implementation is missing anything?
hswolff commentedon Dec 28, 2017
I'm worried I didn't explain the use case well. Maybe @ljharb will be able to?
ljharb commentedon Dec 28, 2017
The issue is that when a project needs to always run a global beforeEach or afterEach, and doesn’t want to force everyone who runs
mochato remember to specify it in the proper order. By allowing it to be a CLI option, it can also go inmocha.opts, which allows it to be encoded in the project instead of in each invocation.(I don’t believe positional arguments work in mocha.opts, or else I’d use that)
boneskull commentedon Dec 30, 2017
A workaround is this:
Where
init-global-hooks.jsis this:before-each.jsandafter-each.js, respectively:test.spec.js:$ mocha --require init-global-hooks.js test.spec.js my suite global before each ✓ should execute both global hooks global after each 1 passing (6ms)ljharb commentedon Dec 30, 2017
I mean, thanks, but I hope you agree that that's a pretty atrociously complex workaround :-) (and it relies on
module.parent, ick)boneskull commentedon Dec 30, 2017
@ljharb yes, I never claimed it was an elegant workaround, but it does accomplish same thing w/o needing to add
commanderas a dependency.Using a positional arg in
mocha.optsmeans you can't use any other positional args when invokingmocha, afaict.I think
--fileas an alias (of sorts) for a positional argument would be reasonable. I think that's all we're really talking about here, yes? The user is still responsible for the order in which the arguments are given:Though specifying
--filemultiple times seems unnecessary in the general sense. Either way, all--fileargs will be run before positional ones.ljharb commentedon Dec 30, 2017
Yes, that sounds like exactly what we'd need here - user-controlled ordering, preferential execution before all positional arguments, and works in
mocha.optswithout interfering with normal usage of the CLI. Thanks!Add ability to pass in test files to be ran before positional files v…
Add ability to pass in test files to be ran before positional files v…
Add ability to pass in test files to be ran before positional files v…