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

Running the test suite twice skips tests that were already required #736

Closed
ghost opened this issue Jan 24, 2013 · 23 comments
Closed

Running the test suite twice skips tests that were already required #736

ghost opened this issue Jan 24, 2013 · 23 comments
Labels
status: accepting prs Mocha can use your help with this one! type: bug a defect, confirmed by a maintainer

Comments

@ghost
Copy link

ghost commented Jan 24, 2013

I wanted to run the test suite twice against different browsers on Saucelabs. I'm using the technique to run mocha programatically but on the second run mocha would always report that it did not run any tests.

After a lot of digging around it seems that since mocha uses require() to load the test files, they get cached and if they're cached they're not added to the suite.

I can manually clear all the tests from the require cache, but I'm wondering if maybe mocha shouldn't do this by default?

This is a simplified example of what I end up doing for it to work:

var Mocha = require('mocha');
var path  = require('path');

var testFile = path.resolve(__dirname, './test_file.js');

var set1 = new Mocha({ui: 'tdd'});
set1.addFile(testFile);

var set2 = new Mocha({ui: 'tdd'});
set2.addFile(testFile);

set1.run(function(failures1){
  console.log(require.cache['/Users/patrick/Downloads/mocha_argh/test_file.js']);
  // Need to delete the test from the cache or else
  // the test won't run.
  delete require.cache['/Users/patrick/Downloads/mocha_argh/test_file.js'];
  set2.run(function(failures2){
    process.exit(failures1 && failures2);
  });
});
@ghost
Copy link
Author

ghost commented Jan 24, 2013

I know it's somewhat of an edge case, but at least maybe something should be added to the documentation to explain that behavior.

@tj
Copy link
Contributor

tj commented Jan 24, 2013

hmm yeah that's not good, I'll try and look into this soon and see where the state is at

@sebv
Copy link
Contributor

sebv commented Oct 23, 2013

+1

3 similar comments
@cruiser12
Copy link

+1

@vbarach
Copy link

vbarach commented Jul 24, 2014

+1

@ririvas
Copy link

ririvas commented Sep 2, 2014

+1

@jreichenberg
Copy link

+1 ... Use case: we are trying to do this in order to run mocha multiple times in a grunt task, against different webdriver configurations

@boneskull boneskull added type: bug a defect, confirmed by a maintainer status: accepting prs Mocha can use your help with this one! labels Nov 6, 2014
@dogeatdog
Copy link

+1, any progress with this ?

@ToshioMagic
Copy link

+1 this would be really useful. Running mocha tests on a backend but when I click a button on the frontend to redo the tests, it refuses because of caching.

@MichaelBorde
Copy link

+1

@db-ryan
Copy link

db-ryan commented Aug 7, 2015

I think it is really easy to miss the workaround in the original post, this seems to work for me...

To add a file programmatically:

var filename = '/Users/patrick/Downloads/mocha_argh/test_file.js';
delete require.cache[filename];
mocha.addFile(filename);

Basically, every time you add a test to your mocha instance, just delete the cache for that file (whether it exists or not).

@boneskull
Copy link
Member

@psturc would @1999's fix in #2435 solve your issue?

@psturc
Copy link

psturc commented Aug 17, 2016

@boneskull Yes, @1999's solution does work for me. 👍

@donaldpipowitch
Copy link

donaldpipowitch commented Sep 23, 2016

Do I read the PRs correctly so this should work when v3.1 is released...? 92f0248#diff-aa849a970cef551664c12f04a4209f6fR354 Will this be called automatically?

Can I run the test suites in parallel? (E.g. it looks like most people in this issue try to run one Mocha test suite multiple times for different browsers in Selenium/Sauce Labs/etc..)

@1999
Copy link
Contributor

1999 commented Sep 23, 2016

Can I run the test suites in parallel

Probably mocha-parallel-tests is what you need?

@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
@boneskull boneskull removed the stale this has been inactive for a while... label Oct 17, 2017
@boneskull
Copy link
Member

not stale

@ghost
Copy link

ghost commented Nov 1, 2017

Weird, I am getting proper (error) output the first time it runs, second time it runs in the same context I get 0 passing like nothing is running.

@ScottFreeCode
Copy link
Contributor

@megamindbrian Sounds like a variation on #2906.

@ghost
Copy link

ghost commented Nov 7, 2017 via email

@akhilesharora
Copy link

+1, any progress with this ?

1 similar comment
@RyanLiuF
Copy link

+1, any progress with this ?

@craigtaub
Copy link
Contributor

Closing as believe it is now fixed (#4234)

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
Projects
None yet
Development

No branches or pull requests