pghalliday / grunt-mocha-test

https://www.npmjs.com/package/grunt-mocha-test
MIT License
362 stars 61 forks source link

Console.log errors and stack traces when they occur (e.g. as a console.log in afterEach) rather than after all tests complete #132

Closed mmcintyre123 closed 7 years ago

mmcintyre123 commented 7 years ago

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:

describe('Group of tests', function () {    
    it('test 1', function () {
        //steps and assertion(s)
    });
    it('test 2', function () {
        //steps and assertion(s)
    });
    it('test 3', function () {
        //steps and assertion(s)
    });
});

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:

 > RESPONSE elementById("Some Id") {"ELEMENT":"9D64458D-AA96-4E04-B4FC-BB8A9A6CC56D"}
 > CALL element.getAttribute("value")
 > GET /session/:sessionID/element/9D64458D-AA96-4E04-B4FC-BB8A9A6CC56D/attribute/value
 > RESPONSE element.getAttribute("value") true
          1) Test that value should be false
 > CALL sleep(1)
 > RESPONSE sleep(1)
 > CALL saveScreenshot("Test that value should be false.png")
 > GET /session/:sessionID/screenshot

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.

mmcintyre123 commented 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.

pghalliday commented 7 years ago

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.