This is a tutorial on using intern to unit/functional test your esri JavaScript API apps.
First off big props to Colin Snover over at sitepen for the intern-tutorial found here. Colins tutorial was my starting point and helped me get the feel for intern and writing the tests themselves. I highly recommend you read his tutorial first, then come back here.
For a fast start running my example tests, follow these steps:
- Clone or download this repo.
- We are going to run our tests with a 'head' (browser) so move or copy the tutorial to a web accessible folder. We will be using the browser test runner so intern needs to be served via a web server to avoid any cross domain errors.
- If you don't have node installed, install it.
- Install intern-geezer in the root of the project:
cd <path to the tutorial>
npm install intern-geezer
- Thats it! your'e done!
- Lets run the tests I provided in this tutorial, open a browser and point it here:
http://<path to the tutorial>/intern-tutorial-esri-jsapi/node_modules/intern-geezer/client.html?config=tests/intern
- you will need to have your console open as this is where the output from intern tests are displayed when using the browser runner (client.html). It should look something like this:
The key to making intern work with the esri jsapi is two fold:
-
You have to use intern-geezer due to a bug in dojo 1.8.3 and below. Esri jsapi 3.6 is based on dojo 1.8.3. If your looking to support old IE (8 and below) you need to use intern-geezer anyway.
-
Defining the intern loader to work with the esri jsapi. Intern uses a local copy of dojo core. As such you need to tell it where to find the esri jsapi. Do this in your intern config file:
loader: {
// Packages that should be registered with the loader in each testing environment
packages: [{
name: 'app',
location: 'app'
}, {
name: 'gis',
location: 'gis'
}, {
name: 'esri',
location: 'http://js.arcgis.com/3.6/js/esri'
}, {
name: 'dojo',
location: 'http://js.arcgis.com/3.6/js/dojo/dojo'
}, {
name: 'dojox',
location: 'http://js.arcgis.com/3.6/js/dojo/dojox'
}, {
name: 'dijit',
location: 'http://js.arcgis.com/3.6/js/dojo/dijit'
}]
}
You also need to add the locations to your custom modules you want to load and test. In the above example 'app' and 'gis' is where we have some modules to test.
- Intern-geezer only supports
intern/chai!assert
. When the esri jsapi moves to 1.8.4 or higher, you can switch to regular intern and take advantage ofintern/chai!expect
andintern/chai!should
. Esri jsapi 3.6 is based on dojo1.8.3
but 3.7 will be based on dojo1.9.1
.
-
tests\hello.js
This test comes from Colins tutorial and is very simple, the hello world of intern. -
tests\extent.js
This test demonstrates using the esri Extent and Point classes from the api. -
tests\map.js
This test demonstrates the async testing method. This will be the most common test method as most apps need to wait for objects to load before they can be used. For exampleesri/map
. -
tests\printWidget.js
This test demonstrates how to use two objects that require waiting until their on load fires before testing. This also shows how to use the async method a little differently thanmap.js
test. For more info on async tests read here. -
tests\functional\index.js
This test comes from Colins tutorial is is the hello world for functional testing.
Intern has docs located in the wiki of the intern repo. Read them. While not comprehensive it does outline everything needed.
Chai doc can be found here.