{"__v":24,"_id":"54d5635732d98b0d00384b17","category":{"__v":4,"_id":"54d5635632d98b0d00384b00","pages":["54d5635732d98b0d00384b17","54ef76a36ce8d81900c1c3dd","554559a635b2240d00838915","5624bfd26ff1010d009b15f9"],"project":"54d53c7b23010a0d001aca0c","version":"54d5635532d98b0d00384afb","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-02-06T23:56:35.650Z","from_sync":false,"order":1,"slug":"development-workflow","title":"Development Workflow"},"parentDoc":null,"project":"54d53c7b23010a0d001aca0c","user":"54cfa8e1a8a4fd0d00b7fd1d","version":{"__v":10,"_id":"54d5635532d98b0d00384afb","forked_from":"54d53c7c23010a0d001aca0f","project":"54d53c7b23010a0d001aca0c","createdAt":"2015-02-07T00:59:01.934Z","releaseDate":"2015-02-07T00:59:01.934Z","categories":["54d5635632d98b0d00384afc","54d5635632d98b0d00384afd","54d5635632d98b0d00384afe","54d5635632d98b0d00384aff","54d5635632d98b0d00384b00","54d5635632d98b0d00384b01","54d5635632d98b0d00384b02","54d652097e05890d006f153e","54dd1315ca1e5219007e9daa","54e21e2b22de1c230094b147","54e68e62a43fe13500db3879","54fa1d3fe7a0ba2f00306309","551c453a23a1ee190034d19a","551df586e52a0b23000c62b6","551f39be6886f8230055f02a","55a6720751457325000e4d97"],"is_deprecated":false,"is_hidden":false,"is_beta":true,"is_stable":true,"codename":"","version_clean":"0.1.0","version":"0.1"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-02-06T23:57:00.771Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"required","params":[],"url":""},"isReference":false,"order":4,"body":"An ecosystem stands and falls by the quality of its packages. Writing tests for software is a common practice that not only ensures quality now, but also that the features of today keep working in the future. To ensure the quality of the tools that we all depend on, each plugin of your package *must* have tests associated with it. This guide outlines how to easily add tests to a package and run them prior to publishing your package.\n\n# Creating a Test\n\nCloud9 packages are tested using [mocha](http://mochajs.org/) and tests are run right within the IDE. This means that all plugins, including the one that is tested can be accessed from your test script. A test script is nothing more than a plugin that executes tests. \n\nCreate a file called `<plugin-name>_test.js`. If your plugin is `terminal.js` call your test file `terminal_test.js`. This is what the basic structure looks like:\n\n```\ndefine(function(require, exports, module) {\n    main.consumes = [\"plugin.test\", \"myplugin\"];\n    main.provides = [];\n    return main;\n\n    function main(options, imports, register) {\n        var test = imports[\"plugin.test\"];\n        var myplugin = imports.myplugin;\n        \n        var describe = test.describe;\n        var it = test.it;\n        var assert = test.assert;\n    \n        describe(myplugin.name, function(){\n            // Your test here\n        });\n        \n        register(null, {});\n    }\n});\n```\n\nBesides `describe()`, `it()` and `assert()`, there are `after()`, `beforeEach()`, `afterEach()` available. Check out the [mocha documentation](http://mochajs.org/) on how to use these. There are also the `assert()` and `expect()` functions. The `assert()` method provides the [same API as provided by Node.js](https://nodejs.org/api/assert.html) and `expect()` is implemented by [chai.js](http://chaijs.com/api/bdd/).\n\nThe following example has some basic tests.\n\n```\ndescribe(myplugin.name, function(){\n    this.timeout(2000);\n    \n    it(\"has a sync test\", function() {\n    });\n    \n    it(\"has a async test\", function(done) {\n        done();\n    });\n    \n    it(\"has a failing test\", function() {\n        assert.equal(10, 11);\n    });\n});\n```\n\n# Running a Test\n\nTests run in the [debug mode](http://cloud9-sdk.readme.io/v0.1/docs/package-development-workflow#cloud9-debug-mode) of Cloud9. Simply load Cloud9 with `?debug=2` behind the url to enable the debug mode.\n\nYou'll find tests of all loaded plugins under `Tools / Developer / Tests` as the picture below shows.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/zpzZJC4XQwCPqWzJyHGK_Screen%20Shot%202015-05-03%20at%208.58.28%20PM.png\",\n        \"Screen Shot 2015-05-03 at 8.58.28 PM.png\",\n        \"2508\",\n        \"1470\",\n        \"#43ba86\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\nRunning a test will open the preview pane and show the test results. Use the refresh button to re-run the tests.\n\n# Publishing\n\nWhen [publishing a package](doc:publishing-packages) the tests for each plugin are run. If they fail the package is not accepted. This protects package builders and users from a buggy experience.\n[block:callout]\n{\n  \"type\": \"danger\",\n  \"body\": \"During the alpha phase of the SDK the tests are not run automatically yet.\"\n}\n[/block]","excerpt":"","slug":"writing-tests","type":"basic","title":"Writing Tests"}
An ecosystem stands and falls by the quality of its packages. Writing tests for software is a common practice that not only ensures quality now, but also that the features of today keep working in the future. To ensure the quality of the tools that we all depend on, each plugin of your package *must* have tests associated with it. This guide outlines how to easily add tests to a package and run them prior to publishing your package. # Creating a Test Cloud9 packages are tested using [mocha](http://mochajs.org/) and tests are run right within the IDE. This means that all plugins, including the one that is tested can be accessed from your test script. A test script is nothing more than a plugin that executes tests. Create a file called `<plugin-name>_test.js`. If your plugin is `terminal.js` call your test file `terminal_test.js`. This is what the basic structure looks like: ``` define(function(require, exports, module) { main.consumes = ["plugin.test", "myplugin"]; main.provides = []; return main; function main(options, imports, register) { var test = imports["plugin.test"]; var myplugin = imports.myplugin; var describe = test.describe; var it = test.it; var assert = test.assert; describe(myplugin.name, function(){ // Your test here }); register(null, {}); } }); ``` Besides `describe()`, `it()` and `assert()`, there are `after()`, `beforeEach()`, `afterEach()` available. Check out the [mocha documentation](http://mochajs.org/) on how to use these. There are also the `assert()` and `expect()` functions. The `assert()` method provides the [same API as provided by Node.js](https://nodejs.org/api/assert.html) and `expect()` is implemented by [chai.js](http://chaijs.com/api/bdd/). The following example has some basic tests. ``` describe(myplugin.name, function(){ this.timeout(2000); it("has a sync test", function() { }); it("has a async test", function(done) { done(); }); it("has a failing test", function() { assert.equal(10, 11); }); }); ``` # Running a Test Tests run in the [debug mode](http://cloud9-sdk.readme.io/v0.1/docs/package-development-workflow#cloud9-debug-mode) of Cloud9. Simply load Cloud9 with `?debug=2` behind the url to enable the debug mode. You'll find tests of all loaded plugins under `Tools / Developer / Tests` as the picture below shows. [block:image] { "images": [ { "image": [ "https://files.readme.io/zpzZJC4XQwCPqWzJyHGK_Screen%20Shot%202015-05-03%20at%208.58.28%20PM.png", "Screen Shot 2015-05-03 at 8.58.28 PM.png", "2508", "1470", "#43ba86", "" ] } ] } [/block] Running a test will open the preview pane and show the test results. Use the refresh button to re-run the tests. # Publishing When [publishing a package](doc:publishing-packages) the tests for each plugin are run. If they fail the package is not accepted. This protects package builders and users from a buggy experience. [block:callout] { "type": "danger", "body": "During the alpha phase of the SDK the tests are not run automatically yet." } [/block]