Q:

How to use chai's expect in nightwatch.js tests

I do integration tests with nightwatch. Some UI automation, which works fine. In the end I want to check using a Rest API, if the results of the UI operations are right. I try to use chai there, as chai is inbuild into nightwatch. Out of the box nightwatch only supports expect.element('#element')..., but I need expect(true).to.be.true style.
This is (part) of my test file:
const { expect } = require('chai');

module.exports = {
    'Add Rules (Tester 1)' : browser => {
         ... some tests
    }
    after: browser => {
        let username = 'username';
        let password = 'xxxxxxxx';

        fetchRulesFromJira({ browser, username, password }).then(rules => {
            expect(rules.length).to.equal(2);
            browser.end();
        });
    }
};

If the expect fails the output is 

(node:3594) UnhandledPromiseRejectionWarning: Unhandled promise
  rejection (rejection id: 1): AssertionError: expected 3 to equal 2

Somehow useful but not nice. If the test succeeds then there is no output.
How can I make nightwatch.js print the results, or any other way to have a more standard output of test results?

A:

I agree, it would be nice if the expect assertions could be used for more than just elements. The workaround I use for this instead is browser.verify.ok() and then I just add a message that describes what the test is actually doing so that when I look at the results I can see what test it was. In your case it would be something like this:
browser.verify.ok(rules.length === 2, 'there are 2 rules');

In the console you will see a message like this:
Passed [ok]: there are 2 rules

Although, I don't think the above approach will handle your promise rejection issue at all. You could just put a catch after the then and that will fix your issue with the unhandled promise rejection. This is the right way to handle rejected promises.
fetchRulesFromJira({ browser, username, password }).then(rules => {
        expect(rules.length).to.equal(2);
        browser.end();
    })
    .catch(err => {
       browser.assert.fail(err);
    });

Also, after looking at your code again I have one last thing to mention. You are trying to do an assertion in an after method. This will run after all the tests have completed. Typically you would use the after function for some kind of cleanup or teardown. I'm still new to nightwatch but I don't think it is best practice to put assertions in there. Also, I'm not entirely sure they even work in the before[Each] or after[Each] functions. If you put that assertion inside a test function you should start seeing the output in the console. 

