Testing Your Package

wagenet edited this page Sep 21, 2011 · 5 revisions

Once you have a package, the next step is to write tests for it. We'll be working off of the package we created in Creating a Package.

Setting Up Your Tests

Start off by creating a tests folder in your package:

mkdir tests

Then create a dummy test:

alert("This is a dummy test");

Next you'll need to let your package know that you have tests.

"bpm:build": {
  "my_package/bpm_tests.js" {
    "files": ["tests"]

This tells BPM that your tests should be compiled into my_package/bpm_tests.js. You can choose a different location if you want, but QUnit (which we'll introduce you to shortly) expects this location.

Now if you open http://localhost:4020/assets/my_package/bpm_tests.js you'll see the dummy test.

Lets update our test.html to include the tests we just created. After the existing <script> tag, add:

<script src="assets/my_package/bpm_tests.js"></script>

If you open test.html in bpm preview you'll now see that your dummy test is being run.

Using QUnit

If you're going to write many tests you'll want some sort of testing library. A good choice is QUnit and it's already supported by BPM. To add QUnit as a dependency for development only, run:

bpm add qunit --development --package

The --development flag will add QUnit as a development dependency. This means that QUnit won't be included in the output of bpm rebuild which builds for production mode (unless you specify --mode=debug). bpm preview, however, does default to debug mode.

You'll also want to write a real test for QUnit:

test("truth", function(){
  ok(true, "truth exists!");

The QUnit package comes with a custom test runner, so we actually don't need to use our test.html. Just head to http://localhost:4020/assets/qunit/index.html?package=my_package and you'll see that QUnit has just run your test!

Once you're pleased with your new package, learn about Distributing Your Package.