javascriptnode.jstypescriptparallel-processingmocha.js

Getting original process.argv value on mocha on parallel run


I just upgraded to mocha 8 and enabled a parallel run of test files. It spawns several browser windows so it's working, but now the process.argv value has changed.

How do I get the original arguments passed to the command?

Here is my .mocharc.json file:

{
 "timeout": 90000,
 "extension": ["ts"],
 "parallel": true,
 "jobs": 4,
 "require": [
   "./tests/config/init.ts",
 ],
 "spec": ["./tests/specs/*.ts"]
}

Initially, when running without parallel mode, the value of process.argv is

['/usr/local/Cellar/node/16.0.0_1/bin/node',
 '/Users/current/proj/node_modules/mocha/bin/mocha',
 '-r',
 '/Users/current/proj/node_modules/ts-mocha/src/index.js',
 './tests/tsconfig.json',
 '--config=./tests/.mocharc.json',
 '--headless',
];

When running in parallel mode, it shows as:

['/usr/local/Cellar/node/16.0.0_1/bin/node',
 '/Users/current/proj/node_modules/mocha/lib/nodejs/worker.js',
];

Which makes sense, since now the process is a worker, but I need to retrieve the original values of process.argv.


Solution

  • At the end, the solution I got from asking in the GitHub project was to attach process.argv to process.env, which by default should be passed to the worker process. This could be done in Mocha's configuration file .mocharc.js. Basically, set the flags you want into an environment variable, and extract it from there.