Skip to content

Latest commit

 

History

History
120 lines (79 loc) · 4.24 KB

testing.md

File metadata and controls

120 lines (79 loc) · 4.24 KB

Testing

When writing unit tests for Shunter-based apps, there will be certain fixtures that require help from Shunter, such as rendering a HTML partial from a Dust template with dummy JSON input, or writing compiled JavaScript into a test runner.

Testing Templates explains how to use Shunter's exported function testhelper that helps you set up and tear down HTML partials.

Testing Client-Side JavaScript explains how to use the shunter-test-client script to run client-side unit tests from the command line.

Testing Templates

As dust templates can contain logic, there will be scenarios where you want to test that your code behaves as expected.

In your test spec, you need to load in the test helper from shunter, as you will need some of shunter's features to render the templates for testing.

Other dependencies are up to you. We use Mocha, but you can use whatever you want.

var assert = require('assert');
var rootdir = __dirname.substring(0, __dirname.indexOf('/tests/'));
var helper = require('shunter').testhelper();

describe('Foo bar', function() {
    before(function() {
        helper.setup(rootdir + '/view/template.dust', rootdir + '/view/subdir/template.dust');
    });
    after(helper.teardown);

    it('Should do something', function(done) {
        helper.render('template', {
            foo: 'bar',
            lorem: 'ipsum'
        }, function(error, $, output) {
            assert.strictEqual($('[data-test="fooitem"]').length, 1);
            done();
        });
    });

In the helper.render callback, the $ parameter is a Cheerio instance which allows you to use jQuery-like functions to access the render output. The output parameter contains the full output as a string.

When testing templates that are in subfolders, be sure to pass in any subfolders in the same way that you'd include a partial

helper.render('mysubfolder___templatename', {
    foo: 'bar'
}, function(error, $, output) {
    // etc etc
});

You can test individual templates by running mocha directly with the command:

./node_modules/mocha/bin/mocha -R spec -u bdd test/myfolders/mytemplate-spec.js

In addition to these tests we recommend using Dustmite to lint your dust files and ensure that they are all syntactically valid.

Testing Client-Side JavaScript

Shunter provides a command-line script that will:

  • build up a test runner page for Mocha-PhantomJS that loads in your JavaScript under test with Mincer, and adds any test specification files found in the folder set in config.path.clientTests (by default, 'tests/client'), and sets up the mocha libraries for client-side testing.
  • run your tests with console output detailing whether they passed or failed.
  • exit to the command line with an exit code of 0 for success and a positive integer for failure so that you can run on CI

This means your code under test is compiled and loaded in the same way it would be when running the app in development mode.

The script makes use of mocha-phantomjs-core, and the test runner page loads in Proclaim as an assertion library.

Before you can use the test runner, you'll need to install PhantomJS separately.

You can run your client-side tests with the command:

./node_modules/.bin/shunter-test-client

Optional Arguments

Test Just One Spec File

./node_modules/.bin/shunter-test-client --spec file-spec

Test In Browsers With Sauce Labs

Requires Sauce Connect, see https://docs.saucelabs.com/reference/sauce-connect/ Once Sauce Connect is installed, you need to run it with:

bin/sc -u YOUR_USERNAME -k YOUR_ACCESS_KEY

Then you can run the command:

./node_modules/.bin/shunter-test-client --browsers

Add A Resource Module

Add a resource module to the JavaScript under test (modules in your config automatically added)

./node_modules/.bin/shunter-test-client --resource-module foo

Related: