1 |
|
2 | Testing
|
3 | =======
|
4 |
|
5 | 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.
|
6 |
|
7 | [Testing Templates](#testing-templates) explains how to use Shunter's exported function `testhelper` that helps you set up and tear down HTML partials.
|
8 |
|
9 | [Testing Client-Side JavaScript](testing-client-side-javascript) explains how to use the `shunter-test-client` script to run client-side unit tests from the command line.
|
10 |
|
11 |
|
12 | Testing Templates
|
13 | -----------------
|
14 |
|
15 | As dust templates can contain logic, there will be scenarios where you want to test that your code behaves as expected.
|
16 |
|
17 | 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.
|
18 |
|
19 | Other dependencies are up to you. We use [Mocha](https://mochajs.org/), but you can use whatever you want.
|
20 |
|
21 | ```js
|
22 | var assert = require('assert');
|
23 | var rootdir = __dirname.substring(0, __dirname.indexOf('/tests/'));
|
24 | var helper = require('shunter').testhelper();
|
25 |
|
26 | describe('Foo bar', function() {
|
27 | before(function() {
|
28 | helper.setup(rootdir + '/view/template.dust', rootdir + '/view/subdir/template.dust');
|
29 | });
|
30 | after(helper.teardown);
|
31 |
|
32 | it('Should do something', function(done) {
|
33 | helper.render('template', {
|
34 | foo: 'bar',
|
35 | lorem: 'ipsum'
|
36 | }, function(error, $, output) {
|
37 | assert.strictEqual($('[data-test="fooitem"]').length, 1);
|
38 | done();
|
39 | });
|
40 | });
|
41 | ```
|
42 |
|
43 | In the `helper.render` callback, the `$` parameter is a [Cheerio](https://github.com/cheeriojs/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.
|
44 |
|
45 | When testing templates that are in subfolders, be sure to pass in any subfolders in the same way that you'd include a partial
|
46 |
|
47 | ```js
|
48 | helper.render('mysubfolder___templatename', {
|
49 | foo: 'bar'
|
50 | }, function(error, $, output) {
|
51 | // etc etc
|
52 | });
|
53 | ```
|
54 |
|
55 | You can test individual templates by running mocha directly with the command:
|
56 |
|
57 | ```sh
|
58 | ./node_modules/mocha/bin/mocha -R spec -u bdd test/myfolders/mytemplate-spec.js
|
59 | ```
|
60 |
|
61 | In addition to these tests we recommend using [Dustmite](https://github.com/nature/dustmite) to lint your dust files and ensure that they are all syntactically valid.
|
62 |
|
63 |
|
64 | Testing Client-Side JavaScript
|
65 | ------------------------------
|
66 |
|
67 | Shunter provides a command-line script that will:
|
68 |
|
69 | * build a test runner page for Mocha-PhantomJS that loads the JavaScript you need to test with Mincer, adds any test specification files found in the folder set in `config.path.clientTests` (by default, 'tests/client'), then sets up the mocha libraries for client-side testing. Dust templates stored in `config.path.templates` (by default: `/view`) will be compiled and exposed to the test runner page. They can be targeted using a combination of their location and name. A template `/view/foo/bar.dust` would be exposed to the test page as `foo__bar` and can be rendered by calling `dust.render('foo__bar', {}, function (err, rendered) {…})` as described in the [relevant chapter of the dust documentation](http://www.dustjs.com/guides/rendering/).
|
70 | * run your tests with console output detailing whether they passed or failed.
|
71 | * 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
|
72 |
|
73 | This means your code under test is compiled and loaded in the same way it would be when running the app in development mode.
|
74 |
|
75 | The script makes use of [mocha-phantomjs-core](https://github.com/nathanboktae/mocha-phantomjs-core), and the test runner page loads in [Proclaim](https://github.com/rowanmanning/proclaim) as an assertion library.
|
76 |
|
77 | Before you can use the test runner, you'll need to [install PhantomJS](http://phantomjs.org/) separately.
|
78 |
|
79 | You can run your client-side tests with the command:
|
80 |
|
81 | ```sh
|
82 | ./node_modules/.bin/shunter-test-client
|
83 | ```
|
84 |
|
85 | ### Optional Arguments ###
|
86 |
|
87 | #### Test Just One Spec File ####
|
88 |
|
89 | ```sh
|
90 | ./node_modules/.bin/shunter-test-client --spec file-spec
|
91 | ```
|
92 |
|
93 | #### Test In Browsers With Sauce Labs ####
|
94 |
|
95 | Requires Sauce Connect, see https://docs.saucelabs.com/reference/sauce-connect/
|
96 | Once Sauce Connect is installed, you need to run it with:
|
97 |
|
98 | ```sh
|
99 | bin/sc -u YOUR_USERNAME -k YOUR_ACCESS_KEY
|
100 | ```
|
101 |
|
102 | Then you can run the command:
|
103 |
|
104 | ```sh
|
105 | ./node_modules/.bin/shunter-test-client --browsers
|
106 | ```
|
107 |
|
108 | #### Add A Resource Module ####
|
109 | Add a resource module to the JavaScript under test (modules in your config automatically added)
|
110 |
|
111 | ```sh
|
112 | ./node_modules/.bin/shunter-test-client --resource-module foo
|
113 | ```
|
114 |
|
115 |
|
116 | ---
|
117 |
|
118 | Related:
|
119 |
|
120 | - [Full API Documentation](index.md)
|