forked from adobe/brackets
-
Notifications
You must be signed in to change notification settings - Fork 278
Extension Unit Tests
Peter Flynn edited this page Feb 20, 2015
·
5 revisions
Brackets includes a simple test-runner for internal use. You can leverage this same tool for any Brackets extensions you write (though not for other projects you're editing in Brackets... yet).
- Learn about the Jasmine unit-test framework.
- Make sure you are running Brackets from git source - the test-runner is not present in the regular download of Brackets. (And running from source is strongly recommended for extension developers anyway).
- Add a
unittests.js
module in the root of your extension folder. - Write Jasmine test cases inside the module, using
describe() {}
blocks. Here's an example. - Choose Debug > Run Tests
- Click the Extensions tab
- Click the name of your Jasmine block to run it
main.js
Start with the code from Simple "Hello World" extension. Then add exports.handleHelloWorld = handleHelloWorld;
to the bottom of the module, inside the curly braces (this exposes the method as an API that unittests.js
can invoke).
define(function (require, exports, module) {
"use strict";
var main = require("main");
describe("Hello World", function () {
it("should expose a handleHelloWorld method", function () {
expect(main.handleHelloWorld).not.toBeNull();
});
});
});
This is not the easiest to use setup yet. Watch out for these gotchas:
- You can open dev tools for the unit test window via its own "Show Developer Tools" button.
- You must disable caching once you open dev tools -- even if you've already done so in the dev tools for the main Brackets window.
- Your unit tests run in the test-runner window by default, so the Brackets UI DOM and certain Brackets modules won't be accessible. This may cause your code to fail.
- You can run tests in a clean Brackets window using
SpecRunnerUtils.createTestWindowAndRun()
, which avoids the above problem. But this setup is not well documented and has its own pitfalls -- your test code needs to be careful about whether it's accessing something in the test-runner window vs. the popup Brackets window (for example,$
vs.testWindow.$
). See ProjectManager-test for example code. Debugging tests inside these windows is also a bit tricky.