Closed mmcintyre123 closed 7 years ago
Well I found my answer! In the afterEach there is an object this.currentTest and it contains error information and other info for the current test. Solution:
afterEach(function() {
if (this.currentTest.state == 'failed') {
console.log('\n' + (this.currentTest.err.message.match(/^.*?(?=Error)/gi)) + '\n')
console.log((this.currentTest.err.message.match(/Error.*/gi)) + '\n')
return driver
.takeScreenshotMethod(thisTest);
}
});
Puts the failing command and error on separate lines.
Glad you found a solution. But if you have other questions about Mocha, it would probably be better to raise them in the Mocha project itself
https://github.com/mochajs/mocha
It might also depend on which reporter you use.
I have a large test suite written in Node.js, using mocha, wd.js, and chai. I have multiple files that contain tests, organized (in each file) like:
etc.
I've written the tests so they are mostly independent of one another, i.e. if one it statement fails, there may be a number of subsequent it statements that should be able to run.
The problem is, when errors occur for any it statement they don't appear until the very end when all tests have finished running. This is a nuisance as sometimes a subsequent test will hang, and the errors will never be printed, or, as is more often the case, a test will fail during execution, and I'll want to work on the issue while the rest of the tests run, but it's very hard to do so with no error or stack trace. My solution until now has been to use the --bail flag for mocha, which stops test execution on the first error. But this slows down debugging tremendously as everything has to be discovered and addressed one at a time. If anyone knows or could suggest how to print the errors as they occur, like access and console.log() them in the afterEach for example, that would be very helpful!
So it would look like this:
the indented test is red on the command line and that's all I see on failures. Ideally it would print the error and stack trace right below that, and then again print all errors and stack traces at the end of the output.