Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loadFilesAsync and unloading files #4924

Closed
fvictorio opened this issue Sep 18, 2022 · 0 comments · Fixed by #4945
Closed

loadFilesAsync and unloading files #4924

fvictorio opened this issue Sep 18, 2022 · 0 comments · Fixed by #4945
Labels
area: node.js command-line-or-Node.js-specific type: feature enhancement proposal

Comments

@fvictorio
Copy link

Is your feature request related to a problem or a nice-to-have?? Please describe.
Before ESM, you could run mocha programmatically several times during the same process (see issues #995 and #2783). For example, this works fine:

async function runTest() {
  const Mocha = require("mocha");
  const mocha = new Mocha();

  const cjsTest = path.join(__dirname, `test.js`)
  mocha.addFile(cjsTest);

  await new Promise((resolve) => {
    mocha.run(resolve)
  });

  mocha.dispose()

}

async function main() {
  await runTest()
  await runTest()
}

main()

If you want to support both CJS and ESM tests though, you need to add a call to loadFilesAsync:

async function runTest() {
  const Mocha = require("mocha");
  const mocha = new Mocha();

  const cjsTest = path.join(__dirname, `test.js`)
  const esmTest =path.join(__dirname, `test.mjs`)
  mocha.addFile(cjsTest);
  mocha.addFile(esmTest);

  await mocha.loadFilesAsync()

  await new Promise((resolve) => {
    mocha.run(resolve)
  });

  mocha.dispose()

}

async function main() {
  await runTest()
  await runTest()
}

main()

And in that case, the tests won't run the second time. This happens because the ESM cache is used, and modules cannot be unloaded from there.

Describe the solution you'd like
As far as I know, there are two ways to deal with this problem:

  • Run the tests in a separate process
  • Use a query string or hash in the URL objects passed to the dynamic import to invalidate the cache

The first option doesn't work for us because of performance reasons, since we need to load our application again, which is kind of slow.

The second option might work. It would leak memory, but that's acceptable in our use case.

The problem is that we don't have any way to hook into the code that converts the given files to URL. If that was possible, then we would be able to pass a random hash to the resolved URLs to make sure that the cache is invalidated.

Maybe a better solution would be to accept URL objects in Mocha.addFile.

Describe alternatives you've considered
Described in the previous section.

Additional context
Our tool is a task runner that, among other things, has a test task that uses Mocha under the hood. We are working on adding support for ESM, but we need the task to work when it's programmatically called more than once during the same execution.

@fvictorio fvictorio added the type: feature enhancement proposal label Sep 18, 2022
@juergba juergba linked a pull request Dec 4, 2022 that will close this issue
@juergba juergba added the area: node.js command-line-or-Node.js-specific label Dec 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: node.js command-line-or-Node.js-specific type: feature enhancement proposal
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants