4
votes

I have a suite of client-side Mocha tests that currently run with the browser test runner. But I also have a suite of server-side Mocha tests that run with the Node.js command-line test runner.

I know I can run the client-side tests from the command-line in a headless browser like PhantomJS (e.g. like this), but they'd still run separately from the server-side tests.

Is there any way to run the client-side tests as part of the command-line run?

E.g. always run both sets of tests, and have one combined output like "all 100 tests passed" or "2 tests failed" — across both client-side and server-side suites.

I imagine if this were possible, there'd need to be some sort of "proxy" layer to dynamically describe to the command-line runner each browser test, and notify it of each result (maybe even any console.log output too) as the tests ran in the browser.

Does there exist anything that achieves this? I've had a hard time finding anything. Thanks!

1

1 Answers

1
votes

I use Zombie for this. There's surely a way to do it with Phantom too. Just write your client-side tests under the same directory as your server-side tests and they'll get picked up by Mocha and executed along with the rest.

I'm not sure whether you need some sample test code but here's some just in case:

var app = require('../server').app; // Spin up your server for testing
var Browser = require('zombie');
var should = require('should');

describe('Some test suite', function () {
    it('should do what you expect', function (done) {
        var browser = new Browser();
        browser.visit('http://localhost:3000', function (err) {
            // Let's say this is a log in page
            should.not.exist(err);
            browser
                .fill('#username', 'TestUser')
                .fill('#password', 'TestPassword')
                .pressButton('#login', function (err) {
                    should.not.exist(err);
                    // etc...
                    return done();
                });
        });
    });
});