mocha
mocha copied to clipboard
unloadFiles throws MODULE_NOT_FOUND in ESM context for files added with relative path
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
Programmatically using mocha.addFile(file)
and mocha.unloadFiles()
in an ES module context throws MODULE_NOT_FOUND
when mocha.addFile(...)
is called with a relative path.
Wrapping the module resolution logic with path.resolve(...)
resolves this issue by ensuring the path is absolute.
Steps to Reproduce
https://repl.it/@christiantjl/MochaFileUnloader#fails.js
node succeeds.js
shows that an absolute path works without issue, and node fails.js
shows that adding with a relative path throws an error.
This can be worked around for the time being by only adding absolute filepaths to Mocha:
mocha.addFile(path.resolve(file))