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

🐛 Bug: Mocha stores instance state in shared global space - ie. the require cache #1938

Open
pghalliday opened this issue Oct 21, 2015 · 5 comments
Labels
status: accepting prs Mocha can use your help with this one! type: bug a defect, confirmed by a maintainer type: cleanup a refactor

Comments

@pghalliday
Copy link

This is reflected in #995 and pghalliday/grunt-mocha-test#108, and many other issues that keep cropping up with watch tasks even though I keep telling people to rerun tests in new processes and not reuse the same process (admittedly this pattern has other problems too)

Basically, you cannot create a new instance of mocha and add the same test files as a previously created instance. This is because require is used to run the test files and when you require them again they hit the require cache and don't run (ie. they fail to register their tests).

IMHO this is a bad design choice as instance state should not be dependent on the global state of the require cache. I suggest that addFile be changed to actually load and evaluate the file and not depend on require.

Rant over ;)

@pelotom
Copy link

pelotom commented Jun 24, 2016

Agreed, this is really bad design and should be fixed.

@dlgltlzed
Copy link

dlgltlzed commented Aug 30, 2016

+1
Is https://nodejs.org/api/vm.html an option?

Edit: The following code works for me. It is possible to remove the test files manually from cache.

var Mocha = require('mocha');
var mocha = new Mocha({});
mocha.addFile('test.js');
mocha.run();

setTimeout(function() {
    delete require.cache[require.resolve('./test.js')];
    var mocha = new Mocha({});
    mocha.addFile('test.js');
    mocha.run();
},1000);

Edit2: Btw, the fields below are present in global space after first run. With a VM, these fields could be attached to a context that is available in a test file, but not in the main execution script.

[ 'before',
  'after',
  'beforeEach',
  'afterEach',
  'run',
  'context',
  'describe',
  'xcontext',
  'xdescribe',
  'specify',
  'it',
  'xspecify',
  'xit' ]

@stale
Copy link

stale bot commented Oct 17, 2017

I am a bot that watches issues for inactivity.
This issue hasn't had any recent activity, and I'm labeling it stale. In 14 days, if there are no further comments or activity, I will close this issue.
Thanks for contributing to Mocha!

@stale stale bot added the stale this has been inactive for a while... label Oct 17, 2017
@pelotom
Copy link

pelotom commented Oct 17, 2017

Poke

@JoshuaKGoldberg
Copy link
Member

Related: #2706

@JoshuaKGoldberg JoshuaKGoldberg added the type: cleanup a refactor label Dec 27, 2023
@JoshuaKGoldberg JoshuaKGoldberg changed the title Mocha stores instance state in shared global space - ie. the require cache 🐛 Bug: Mocha stores instance state in shared global space - ie. the require cache Dec 27, 2023
@JoshuaKGoldberg JoshuaKGoldberg added the status: accepting prs Mocha can use your help with this one! label Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: accepting prs Mocha can use your help with this one! type: bug a defect, confirmed by a maintainer type: cleanup a refactor
Projects
None yet
Development

No branches or pull requests

5 participants