We've recently started work on our first Durandal project, so I thought it would be good to contribute back a demonstration of how we test our application. I've created a test suite for the HTML starter kit, which you can see here:
To get it running, you just need to go to browse to /test.html in the HTML starter kit. If you are using firefox, you can do this from the file system. You should see a scenario HTML page that has been generated from a previous test run. It shows the status of all current scenario and steps, with stack traces for errors and allows you re-run any test in the browser (opens a new tab). The tests output to the console by default, so open dev tools/firebug and you can see the test running, insert breakpoints, etc.
Durandal's compositional approach gives a unique opportunity to test applications in a different way from most other frameworks. It makes it easy to test modules in isolation, which is key to creating a maintainable test suite. To facilitate this, I created a test framework that makes it easy to test durandal applications:
The main idea behind the test runner that comes with the test framework is that we should be able to run tests both in the browser and headless via a console runner using phantomjs. To setup testing for your own application, have a look at
main-test.js. This shows how we add some extra paths for the tests, the test library and mocked modules (in this case the http module). You will notice on line 44, we call testRunner.run() rather than app.setRoot. This is so we can "hook" into the application and have the tests drive the app. The
test runner uses window.location.hash to denote which test to run, which will be the id of the test module. Note this does not use routing module and you can still test the routes of you application. You should be able to see from an individual test what is going on, eg
flickr page test.
The
ScenarioRunner.exe is a console app that allows you to run the tests in parallel, report to team city, output the scenario.html and take screenshots.
It's a bit rough around the edges, but hopefully is of some use. If there is any interest I can write a blog post with more detail.
Cheers,
Jon