Jasmine Best Practices

  1. The file name should be Matching for the top level test suite name.
  2. There should be one top level test suite per file.
  3. Give a meaningful name or title for the test suite.
  4. Either use var or this to define variables that needs to use across tests. var variables should be placed just below the top describe (test suite)
  5. Use setup and teardown methods to perform common variable initializations and functionalities.
  6. A spec (test case) should test only “ONE” expectation.
  7. Group the main test suite with sub test suites to group sections.

Good Test Suite

Following is a structure of a good looking Jasmine test suite.

// GoodTestSpec.js
 * Describe the purpose of this test suite here.

// give a meaningful name for the test suite.
// only "ONE" top level test suite per file.
describe('Good test suite', function () {
    // define "ALL" the values that needs to be accessed across test cases
    // Else use `this` keyword. But follow one approach. If `this` is used, don't define values here.
    var foo = null,
        bar = null,
        numberOne = null;

    // initialize "ALL" values above to their desired default value, in this block "beforeEach"
    beforeEach(function () {
        foo = null;
        bar = null;
        numberOne = 1;

        // Using `this` approach. Pick one that suits the Test Suite.
        this.moo = null;

    // a spec (test case) should test only "ONE" expectation
    it('should check foo to be null', function () {
        // ....

    // another spec
    it('should check numberOne to equal 1', function () {
        // call fail() if something is not right and want to fail the test
        // fail('woops! not correct state.');


    // Group the main test suite with sub test suites to group sections.
    // The title should be descriptive.
    describe('that is magnificent', function () {

        // Test name will read as "Good test suite that is magnificent should check 42 to is 42"
        it('should check 42 is 42', function () {

Learn More