-
-
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
Add new --add-file CLI flag #3181
Comments
The CLI expects a list of files as arguments, which are added in order. $ mocha test/hooks.js tests/**/*.test.js --require=babel-register This will run the Can you elaborate if this solves your issue? or if this implementation is missing anything? |
I'm worried I didn't explain the use case well. Maybe @ljharb will be able to? |
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 (I don’t believe positional arguments work in mocha.opts, or else I’d use that) |
A workaround is this: $ mocha --require init-global-hooks.js test.spec.js Where module.parent.require('commander').args.unshift('before-each.js', 'after-each.js');
beforeEach(function () {
console.log('global before each');
}); afterEach(function () {
console.log('global after each');
});
describe('my suite', function () {
it('should execute both global hooks', function () {
});
}); $ 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)
|
I mean, thanks, but I hope you agree that that's a pretty atrociously complex workaround :-) (and it relies on |
@ljharb yes, I never claimed it was an elegant workaround, but it does accomplish same thing w/o needing to add Using a positional arg in I think $ mocha --file loaded-first.js --file loaded-second.js Though specifying |
Yes, that sounds like exactly what we'd need here - user-controlled ordering, preferential execution before all positional arguments, and works in |
…ia --file (mochajs#3190) * Add ability to pass in test files to be ran before positional files via --file Fixes mochajs#3181
Prerequisites
common mistake
labelnode 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 mochamocha 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
andafter
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 themocha.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.The text was updated successfully, but these errors were encountered: