Skip to content
Web App Solution, Inc edited this page Apr 5, 2017 · 10 revisions

Back to Wiki Home

Jasmine

We've chose Jasmine for a unit testing framework. Jasmine is a behavior driven development (BDD) framework. It does not depend on any other JavaScript frameworks. It does not require a DOM. And it has a clean, obvious syntax so that you can easily write tests. It allows your tests to be run in a lot of other environments - Rails, PHP, Node.js, Palm webOS, and even iPhone (say PhoneGap).

For developers familiar with JUnit, FlexUnit, or unit testing frameworks for other languages, Jasmine is a fairly trivial framework to understand.

Where in Flex we had:

package applicationTest._suites_
{
    import applicationTest.AppTest;
     
    [Suite]
    [RunWith("org.flexunit.runners.Suite")] 
    public class AppTestSuite {
         
        public var appTest:AppTest;
  
    }
}

We have even simpler syntax in a file called AppTestSuite.js:

describe("App Test Suite", function() {
   ...
});

In Flex we'd now create separate classes to be added to the suite and add the test functions using the [Test] annotation:

   public class AppTest {
         
        public function AppTest() { }
         
        [Test]
        public function trueIsTrue():void{
            
            Assert.assertTrue(true);
           
        }
    }

In Jasmine we have Specs in place of [Test] annotated functions. Specs are defined by calling the global Jasmine function it, which, like describe takes a string and a function. The string is a title for this spec and the function is the spec, or test. A spec contains one or more expectations that test the state of the code under test.

describe("App Test Suite", function() {
    it("true Is True", function() {
        expect(true).toBe(true);
    });
});

In Flex Unit, you could create variables and control the scope and lifetime of them:

   public class AppTest {
         
        public var app:App;
         
        public function AppTest() { }

        [Before]
        public function setUp():void {
            this.app = new App(); 
        }
         
        [After]
        public function tearDown():void {
            this.app = null; 
        }
         
        [Test]
        public function appHasBeenCreated():void{
            Assert.assertTrue(this.app != null);
        }
    }

Well you can do the same thing in Jasmine and it even looks the same:

describe("App Test Suite", function() {

    var app = null;

    // just like [Before]
    beforeEach(function() {
         app = Ext.create("MyApp");
    }

    // just like [after]
    afterEach(function() {
         app = null;
    }

    it("app Has Been Created", function() {
        expect(app).toBe(!null);
    });
});

Setup

See the Jasmine docs for all of the various configurations, filters, and report types Jasmine supports: