-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
The same test suite won't run twice when using mocha programmatically #995
Comments
I'm also facing this issue. |
Move the |
yep, you can do: var Mocha = require('mocha');
var mocha = new Mocha();
mocha.addFile('test.js');
function runMocha(callback) {
mocha.run(function () {
callback();
});
}
runMocha(function () {
runMocha(function () {});
}); |
Yes it works for this simple example, but what if I don't have control over when mocha is instantiated (i.e. when using a grunt plugin) or i only want to execute a subset of the tests? BTW: In your example I would expect the tests in 'test.js' to run twice on the second run, since I called addFile twice (and it will be contained twice in this.files in mocha.) |
both of those are separate from this. for running a subset of tests in a file you wouldn't do anything different than you'd do anyway i.e. either use describe, it.skip or something. for the grunt thing, post a real example. |
Anyone ever solve this issue? I have the same problem as the OP, can't run a test twice, it runs once and then on the second run does nothing. |
It appears to be because the tests have a function resetTests(suite) {
suite.tests.forEach(function(t) {
delete t.state;
t.timedOut = false;
});
suite.suites.forEach(resetTests);
}
resetTests(mocha.suite);
mocha.run(); |
Just added real example for "grunt thing", but I think this should be fixed on grunt plugin side. So the issue in the plugin repository. |
I just realised that |
How do I run tests more than once?
Output is something like:
|
Same issue, basically. |
Any updates on what @moglars pointed out? |
So I definitely had to solve this problem at least once before, yet tonight I could not remember how, so here is an example to assist my future self (or anyone else 😃 ): function runMochaTests() {
Object.keys( require.cache ).forEach( function( file ) {
delete require.cache[ file ];
} );
var mocha = new Mocha();
mocha.addFile( 'run_test.js' );
mocha.run();
} Its ugly, but you should be able to call |
unless we delete the `require` cache. See here: mochajs/mocha#995 Fixes #325
@traviswimer the problem with your solution is that it seems to reset the local module variables in the required modules (i.e. it clears the require cache - which I don't want to happen). Although, your solution does work, it seems to have the side effects. |
If anyone comes across this post and is trying to run mocha tests in your browser, here is what I did to reset the tests after mocha.run()
|
Hello. The solution from @traviswimer is not working. My mocha tests are running only once. I can not debug it. I dont now solution. |
@MarcelGeo did u find the solution here |
I am facing a similar issue and the solution suggested to delete the cache is not working. |
I almost lost all of my hair. The issue is stated at the bottom of the 'angelo' npm module description, https://www.npmjs.com/package/angelo. The test files are being loaded with a require, which is being cached. A simple fix would be to remove the test files cached after each run. I wouldn't consider this hacky. For my own quick workaround, I'm just calling |
I am also experiencing this issue! |
You should execute |
Deleting the require.cache did not work for me. Spawning a child process did. For my use case, here was the before and after: const testFile = (filename, reporter) => {
if (filename.includes('.test.js')) {
const output = [];
const mocha = new Mocha({ reporter: reporter });
delete require.cache[filename]
mocha.addFile(filename);
return new Promise((resolve, reject) => {
mocha.run(failures => {
resolve(failures);
})
})
}
}; AFTER: const spawn = require('child-process-promise').spawn;
const testFile = (filename, reporter) => {
if (filename.includes('.test.js')) {
const output = [];
const promise = spawn('mocha', ['-R', reporter, filename], {stdio: "inherit"})
return new Promise((resolve, reject) => {
promise
.then(() => resolve(0))
.catch((err) => {resolve(1) })
})
}
}; |
Faced the same issue, and options above have not worked for me. // Usage
const runner1 = new MochaRunner();
const runner2 = new MochaRunner();
runner1.run(['test1.js', 'test2.js']).then(() => {
runner1.cleanup();
});
runner2.run(['test1.js']).then(() => {
runner2.cleanup();
}) and in both cases P.S. Hope it will help to someone 😉 |
Deleting the cache right after running require() seems to do nicely. I suspect it's very racy but I've been able to run the test hundreds of time in parallel. let mocha = new Mocha({});
mocha.suite.on('require', function (global, file) {
delete require.cache[file];
});
//TODO: add files to suite & do things
mocha.run(); |
If anyone is getting node warnings about EventListener leaks on |
In case anyone interest, I find another work around to resolve this problem. It's inspired by @colinbendell . Here is my solution:
Things I tried but didn't work in 9.2.0
|
Calling |
When trying to execute a test suite twice, using the programmatic approach, the second time it should be executed it isn't found. This is especially annoying when trying to execute the same test with differing configuration.
Minimal Example (the
test/test.js
file contains a suite with a single always-passing test):The first time mocha is run it will execute the test, the second time it will not.
The cause for this is that the second time it is required in mocha.js, the file is already cached and won't be executed. The only solution I was able to find is deleting the cached file from
require.cache
, but that doesn't look like good practice to me at all.The text was updated successfully, but these errors were encountered: