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

ESM Import is cached when re-using the process when using mocha programmatically #4655

Closed
smcenlly opened this issue Jun 8, 2021 · 3 comments
Labels
status: waiting for author waiting on response from OP - more information needed

Comments

@smcenlly
Copy link

smcenlly commented Jun 8, 2021

Hey guys - congrats on releasing v9! 🎉

Our product (Wallaby) uses mocha programmatically and by default reuses the node process between test runs. In the latest version of mocha (v9) subsequent changes to a test file are not executing after the first test run. It appears that the import only happens on the first test run which means that on subsequent calls, the context functions (it, describe, etc.) are not re-executed and test execution bails.

We are not sure if this problem is specific to how Wallaby is calling mocha but we would not have thought so. In the past, we have had to add a cache breaker in the past for other parts of Wallaby and Quokka that natively support ESM. Specifically, this line of code is causing the problem:

  // https://github.com/mochajs/mocha/blob/master/lib/esm-utils.js#L7
  return await import(url.pathToFileURL(file));

For now, we have patched this ourselves by adding a cache breaker as we have in other parts of Wallaby (same technique as described in this blog post). So the code becomes:

  return await import(url.pathToFileURL(file) + "?update=" + Date.now());
@smcenlly
Copy link
Author

smcenlly commented Jun 18, 2021

After following our suggested technique, we had a problem with the change we suggested. The query string needs to be added after the pathToFileUrl. Have updated the original issue description so that it is now correct.

@juergba
Copy link
Contributor

juergba commented Jun 21, 2021

@smcenlly thank you.

Mocha is supporting Node's native ESM support since v7.1.0. The ESM loader uses its one cache, different to require.cache of CJS files. Node doesn't offer any API to clear the ESM cache, and afaik they will probably never do.
For that same reason we don't support watch mode with ESM test files, yet. We have no solution to clear the cache either, and we probably will have to spawn a new process for each watch re-run.

In the latest version of mocha (v9) [...]

There shouldn't be any changes refering to ESM cache from v8.4.0 to v9.0.0. Is there any different behavior?

@juergba
Copy link
Contributor

juergba commented Jul 31, 2021

There shouldn't be any changes refering to ESM cache from v8.4.0 to v9.0.0. Is there any different behavior?

I'm going to close this issue within the next few days.

@juergba juergba added the status: waiting for author waiting on response from OP - more information needed label Jul 31, 2021
@juergba juergba closed this as completed Aug 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting for author waiting on response from OP - more information needed
Projects
None yet
Development

No branches or pull requests

2 participants