mocha
mocha copied to clipboard
Test Context not available in Custom Reporter when run in Parallel
Prerequisites
- [X] Checked that your issue hasn't already been filed by cross-referencing issues with the
faq
label - [X] 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.
- [X] '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
- [X] 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 that you not install Mocha globally.
Description
I'm trying to access a test's context (ctx
) in a custom reporter that I'm using for some test-specific data. However, when I run tests in parallel, the context isn't available on the test object. When I run tests sequentially (--no-parallel
), then the context is available.
Steps to Reproduce
I created a repro that reproduces this issue. https://github.com/DanLomanto/mocha-parallel-reporter-test-context
Steps to Reproduce:
- Download the repo.
- Run
yarn install
- To reproduce the failure, run
yarn test
(tests run in parallel) - To see the things working, run
yarn test --no-parallel
(tests run sequentially)
Expected behavior: The test context to be available in a custom reporter when I'm running tests in parallel or sequentially.
Actual behavior: The test context is only available in a custom reporter when running tests sequentially.
Reproduces how often: Every time.
Versions
- The output of
mocha --version
andnode node_modules/.bin/mocha --version
: 8.4.0 - The output of
node --version
: v14.16.0 - Your operating system
- name and version: Mac OSX 10.15.7
- architecture (32 or 64-bit): 64bit
- Your shell (e.g., bash, zsh, PowerShell, cmd): zsh
- Your browser and version (if running browser tests): N/A
- Any third-party Mocha-related modules (and their versions): chai (v4.3.3)
- Any code transpiler (e.g., TypeScript, CoffeeScript, Babel) being used (and its version): Typescript (v4.2.4)
In parallel mode the events have to be de-/serialized in order to be transmitted via IPC. Not all event properties can be considered as serialization is a slow process. In lib/test.js you can see that ctx
is not one of the selected properties.
This issue is not a bug, but made by design, otherwise with too many serialized properties the entire gain in performance would vanish. The parallel mode has its severe drawbacks.
@juergba Thanks for the response!
Do you know of any other way to send test-specific metadata to a custom reporter then, by any chance?
Thanks!
not only is the test.ctx
missing, but also the events aren't run in order. You'll find that your EVENT_TEST_BEGIN
listeners are called after your EVENT_TEST_PASS
, EVENT_TEST_FAIL
and EVENT_TEST_PENDING
listeners for a given test. This essentially negates the usability of a custom Mocha Reporter when running in parallel as you not only lose contractual data (the context), but also lose ordered reporting.