I am building a web app using BackboneJS and RequireJS and need to implement some form of unit testing for UI interaction and data retrieval via AJAX. I have e across QUnit and Jasmine but don't really know how I can integrate this into my app.
If I am testing things such as:
- Is the user logged in alright?
- Has the data been received from the server ok?
- Does clicking a button trigger the expected response?
- Do click events work on dynamically loaded html content?
- Does the app respond correctly to changes in hash/push-state urls?
I would imagine the testing has to be directly integrated into my app so as to have access to specific JS objects, work with session specific data and respond to changes in push state URLs.
How can I integrate QUnit or Jasmine (or other suggestions) into my modular app to unit test such features?
I am building a web app using BackboneJS and RequireJS and need to implement some form of unit testing for UI interaction and data retrieval via AJAX. I have e across QUnit and Jasmine but don't really know how I can integrate this into my app.
If I am testing things such as:
- Is the user logged in alright?
- Has the data been received from the server ok?
- Does clicking a button trigger the expected response?
- Do click events work on dynamically loaded html content?
- Does the app respond correctly to changes in hash/push-state urls?
I would imagine the testing has to be directly integrated into my app so as to have access to specific JS objects, work with session specific data and respond to changes in push state URLs.
How can I integrate QUnit or Jasmine (or other suggestions) into my modular app to unit test such features?
Share Improve this question asked Nov 21, 2011 at 12:29 wilsonpagewilsonpage 17.6k23 gold badges105 silver badges150 bronze badges3 Answers
Reset to default 4Unit testing is really simple.
You make a test HTML page. You include QUnit/NodeUnit/Jasmine/TestLibraryOfChoice
You then use requireJS and load one of your javascript modules,
and you simply test the exported object or function. That means testing the valid inputs of your module and asserting the outputs are correct.
You may have to mock out ajax and write HTML mocks
Dojo Objective Harness (DOH) is a very good unit test framework, which is browser agnostic and supports testing asynchronous functions, see here for a walkthrough guide.
However, from your test cases it looks like you want something more like an integration test? If so Selenium is a good browser automation tool.
Crucially, neither of these tools will require you to modify your code (unless you find bugs :))
If you want to see an example where requireJS based modules are unit tested with QUnit, download the javascript reference architecture at http://boilerplatejs.
Disclaimer: I'm the main author of it.