• Skip to main content
  • Select language
  • Skip to search
MDN Web Docs
  • Technologies
    • HTML
    • CSS
    • JavaScript
    • Graphics
    • HTTP
    • APIs / DOM
    • WebExtensions
    • MathML
  • References & Guides
    • Learn web development
    • Tutorials
    • References
    • Developer Guides
    • Accessibility
    • Game development
    • ...more docs
B2G OS
  1. MDN
  2. Archive of obsolete content
  3. B2G OS
  4. Automated Testing of B2G OS
  5. Writing Gaia Unit Tests

Writing Gaia Unit Tests

In This Article
  1. Writing unit tests
  2. File naming
    1. Examples
    2. Example Implementation (gist):
    3. Example Test (gist):

Writing unit tests

The unit test runner is mocha using the TDD interface. Mocha doesn't ship with an assertion library (there is no ok or assert_equals), so we use chai to provide assertions.

It's highly recommended that you read though the mocha site, since all tests are really mocha tests. The documentation here is focused on getting you started, and about our special integrations with test-agent and Gaia.

It's also important to note that we add special functions (like require() and requireApp()) to make writing tests easier. All test helper scripts can be found in the /common/test directory.

File naming

Tests are usually one to one. One implementation lives in the js/ directory, and one test lives in the test/ directory.

Examples

Implementation Test
apps/app/js/file.js apps/app/test/unit/file_test.js
apps/app/js/nested/thing/file.js apps/app/test/unit/nested/thing/file_test.js

Example Implementation (gist):

//apps/example/js/example.js
var Example = (function(){
  return {
    math: function() {
    },
    complexMethod: function() {
    },
    asyncMethod: function(callback) {
    }
  }
}());

Example Test (gist):

//apps/example/test/unit/example_test.js
requireApp('example/js/example.js');
//suite/setup/test/done are standard mocha functionality.
suite('Example', function() {
  var subject;
  //will be called before each "test" block. 
  setup(function() {
    subject = Example();
  });
  //for a simple method
  test('#math', function() {
    var result = subject.math('1', '+', '1');
    //assert functionality is provided by chai
    assert.equal(result, 2, 'addition should work');
  });
  //there is full support for async tests using done
  //when you set an argument to your test function it is
  //assumed that the given test is async and will only
  //complete once done is called.
  test('#asyncMethod', function(done) {
    subject.asyncMethod(function(err, value) {
      done(function() {
        assert.ok(value, 'sending message failed');      
      });
    });
  });
  //when you have a method that will
  //require complex setup/teardown logic
  suite('#complexMethod', function() {
    var result;
    setup(function() {
      //complex setup stuff
      result = subject.complexMethod();
    });
    test('stuff works', function() {
      assert.typeOf(result, 'string');
      //it is good practice to add the third argument which is
      //a description of why a test failed.
      assert.equal(result, 'real value', 'should output real value');
    });
  });
});

Document Tags and Contributors

Tags: 
  • Firefox OS
  • Gaia
  • Guide
  • Testing
  • unit tests
 Contributors to this page: chrisdavidmills, freddyb, GiovanniC, teoli, kscarfone, espressive
 Last updated by: chrisdavidmills, Feb 27, 2017, 3:02:09 AM
See also
  1. Build and install
    1. Build and install overview
    2. B2G OS build process summary
    3. Build prerequisites
    4. Preparing for your first build
    5. Building B2G OS
    6. B2G installer add-on
    7. Building B2G OS for Flame on Mac OS X
    8. Choosing how to run Gaia or B2G OS
    9. Compatible Devices
    10. Installing B2G OS on a mobile device
    11. Creating and applying B2G OS update packages
    12. Building and installing FOTA community builds
    13. B2G build variables reference sheet
  2. Porting B2G OS
    1. Porting overview
    2. Porting basics
    3. Porting on CyanogenMod
  3. Developing Gaia
    1. Developing Gaia overview
    2. Running the Gaia codebase
    3. Run Gaia on desktop using Mulet
    4. Understanding the Gaia codebase
    5. Making Gaia code changes
    6. Testing Gaia code changes
    7. Submitting a Gaia patch
    8. Gaia build system primer
    9. Different ways to run Gaia
    10. Make options reference
    11. Gaia tools reference
  4. B2G OS APIs