Closed petterm-wwt closed 2 years ago
I've dug into this more and found that there is indeed a bug here.
Parallel works in the simplest case with Javascript; however, I am using Typescript and it doesn't work in parallel, while it works in serial. I believe I have tracked this down to the root cause:
In parallel mode, in Mocha.run(...)
, the root Mocha instance is used to load and configure the tests, and the parallel worker
instances then each create their own Mocha instances, which are used to run the tests in separate processes:
const mocha = new Mocha(opts).addFile(filepath);
(source)
Note that the only means provided for the mocha configuration to be shared from the root Mocha instance to the worker instances is the options
object.
However, in this extension, requires are only loaded for the root Mocha instance, and not added to the options
object on the root Mocha instance, and thus not inherited by the workers:
let requires = []
for (let req of args.mochaOpts.requires) {
if (fs.existsSync(req) || fs.existsSync(`${req}.js`)) {
req = path.resolve(req);
}
if (requireOrImport) {
if (args.logEnabled) sendMessage(`Trying requireOrImport('${req}')`);
requires.push(await requireOrImport(req));
} else {
if (args.logEnabled) sendMessage(`Trying require('${req}')`);
requires.push(require(req));
}
}
const mocha = new Mocha();
...
for (const req of requires) {
if (req.mochaHooks) {
mocha.rootHooks(req.mochaHooks);
}
if (req.mochaGlobalSetup) {
mocha.globalSetup(req.mochaGlobalSetup);
}
if (req.mochaGlobalTeardown) {
mocha.globalTeardown(req.mochaGlobalTeardown);
}
}
And this means that the parallel worker processes' Mocha instances will not have the same require
s available.
Since Typescript uses ts-node/register
as a require
, and this does not get inherited by parallel worker processes, they are failing internally when loading the spec files with ERR_UNKNOWN_FILE_EXTENSION
.
Thus, no tests run.
This extension needs to be updated to ensure that require
s loaded are handed down to the parallel worker processes through the options object.
I tested hacking this line in the extension:
const mocha = new Mocha();
to:
const mocha = new Mocha({require: args.mochaOpts.requires});
and it made the tests run.
Fixed in 2.13.4
Parallel tests aren't running in the test explorer, for "all" or for any individual tests.
They run fine in parallel on the command line, AND they run fine serially in this test adapter when NOT using parallel mode.
But when I add
"mochaExplorer.parallel": true
to my settings, no tests will run. The output in the Test Explorer panel shows no error, just that no tests actually ran:I am using node v16.14.0 and mocha 9.2.0.