Explore key aspects of advanced Mocha configuration to optimize test runs, handle custom interfaces, and manage complex test setups. This quiz helps deepen your understanding of configuration files, parallel execution, hooks, reporters, and advanced timeout control in Mocha's ecosystem.
When configuring Mocha, which file extension is NOT natively supported for specifying test settings without additional loaders or plugins?
Explanation: .js, .json, and .yaml (or .yml) are all supported by Mocha as configuration files directly or via built-in parsing. The .py extension refers to Python files, which are not supported by Mocha natively and would not be recognized without external tooling. Choosing .py would require custom processing, while the other extensions are common configuration formats for this testing tool.
Which Mocha CLI flag should you use to enable parallel execution of test files when running a large suite?
Explanation: The --parallel flag is specifically designed to allow Mocha to execute test files concurrently, speeding up large test suites. --recursive allows test discovery in subdirectories but does not affect execution mode. --growl enables desktop notifications, while --compilers is used for specifying file transpilers. Only --parallel activates parallel processing.
How can you instruct Mocha to use a custom interface, such as 'my-ui', for organizing your test syntax?
Explanation: The --ui flag tells Mocha to use a specific user interface, like 'bdd', 'tdd', or any custom alternative like 'my-ui'. --require loads modules before tests but does not change the interface style. --timeout adjusts test timeouts, and --grep is for pattern-matching test names. Only --ui directly controls the testing interface.
What is the correct way to define a global 'beforeEach' hook across all test files using a Mocha configuration file?
Explanation: To apply a hook globally, you should create a helper file that defines the 'beforeEach' function and then list this file under the 'require' property in your configuration. Placing the function directly under a 'hooks' or 'spec' property is not recognized by Mocha. Setting 'beforeEach' at the top of the config is also invalid, as Mocha loads hooks only from required modules or within test files.
If Mocha is run with a global timeout of 4000 ms, but a test uses 'this.timeout(1000)' within its callback, how long will Mocha wait before failing that test?
Explanation: A timeout specified with 'this.timeout(1000)' inside a test overrides the global or configuration-level timeout for that particular test, so Mocha will only wait 1000 milliseconds before failing it. The values 4000 ms and 5000 ms reference possible global configurations but do not apply when overridden locally. 2000 ms is an unrelated value in this context.