Let's say you have a simple mocha test:
describe("Suite", function(){
it("test",function(doneCallback){
// here be tests
});
});
In this test I can change the timeout by adding this.timeout(VALUE);
anywhere within the describe
function.
However, besides the timeout
value, there are plenty of other Mocha options that can be exclusively declared either from the command line or from a mocha.opts
file that lives in the test folder (./test/mocha.opts
).
What I want is to change some of these options at run-time (for example, the reporter
) and not in command line / mocha.opts
file.
From my research of what's possible, I found that there is an article explaining how you can use mocha programmatically, which would allow changing these options at run-time, but you need to create the Mocha
instance yourself, whereas in an ordinary test one doesn't have direct access to the Mocha
instance.
So, is there a way to get the Mocha
instance from an existent test and change some of these options like reporter
at run-time during a test?
I would like to have an option that doesn't require to modify the source code of Mocha
in any way (I suppose I could tamper with the Mocha
instance to implement a way to get an instance directly in the Mocha
constructor).
The best way that you can achieve that is by using Mocha as per the wiki link that you have already referenced, which is using Mocha programmatically.
So to your inquiry on changing the
reporter
parameter here is a brief example that would do what you want, in order to run the tests against a theoretically already existing file namedtest-file-a.js
that contains your tests:Besides that there are plenty other options that you can use and also there are some listeners for events, like
test
that you may want to do something during a test, for example:Please note that you can define the options per each Mocha instance that will run again a test suite, but not during the runtime of a test suite, so for example if you start your tests for
test-file-a.js
with the optionreporter('list')
as above you cannot change it while the tests are running to something else, like you may do for example with thetimeout
option where you can dothis.timeout()
.So you would have to instantiate a new
Mocha
instance as the examples above with different options each time.No, you cannot. without changing the code.
In short, mocha is created in a scope you cannot access from tests. Without going in details, the objects provided in your scope cannot change the options you want. (You cannot do this: link)
But there is a way to define your own reporter and customize the output for each test:
Create a file called MyCustomReporter.js:
When you run mocha, pass the path of MyCustomReporter.js as reporter parameter(without .js), eg:
The default mocha script actually tries to require a reporter file if it is not found in the default ones(under lib/reporters), github link
Finally, in your tests, you can pass some parameters to customize the output of your reporter: