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.

{#user-content-good-test-suite.anchor}Good Test Suite

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

// 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;

<span class="pl-c">// initialize "ALL" values above to their desired default value, in this block "beforeEach"</span>
<span class="pl-en">beforeEach</span>(<span class="pl-k">function</span> () {
    foo <span class="pl-k">=</span> <span class="pl-c1">null</span>;
    bar <span class="pl-k">=</span> <span class="pl-c1">null</span>;
    numberOne <span class="pl-k">=</span> <span class="pl-c1">1</span>;

    <span class="pl-c">// Using `this` approach. Pick one that suits the Test Suite.</span>
    <span class="pl-v">this</span>.<span class="pl-smi">moo</span> <span class="pl-k">=</span> <span class="pl-c1">null</span>;
});

<span class="pl-c">// a spec (test case) should test only "ONE" expectation</span>
<span class="pl-en">it</span>(<span class="pl-s"><span class="pl-pds">'</span>should check foo to be null<span class="pl-pds">'</span></span>, <span class="pl-k">function</span> () {
    <span class="pl-c">// ....</span>
    <span class="pl-en">expect</span>(foo).<span class="pl-en">toBe</span>(<span class="pl-c1">null</span>);
});

<span class="pl-c">// another spec</span>
<span class="pl-en">it</span>(<span class="pl-s"><span class="pl-pds">'</span>should check numberOne to equal 1<span class="pl-pds">'</span></span>, <span class="pl-k">function</span> () {
    <span class="pl-c">// call fail() if something is not right and want to fail the test</span>
    <span class="pl-c">// fail('woops! not correct state.');</span>

    <span class="pl-en">expect</span>(numberOne).<span class="pl-en">toEqual</span>(<span class="pl-c1">1</span>);
});

<span class="pl-c">// Group the main test suite with sub test suites to group sections.</span>
<span class="pl-c">// The title should be descriptive.</span>
<span class="pl-en">describe</span>(<span class="pl-s"><span class="pl-pds">'</span>that is magnificent<span class="pl-pds">'</span></span>, <span class="pl-k">function</span> () {

    <span class="pl-c">// Test name will read as "Good test suite that is magnificent should check 42 to is 42"</span>
    <span class="pl-en">it</span>(<span class="pl-s"><span class="pl-pds">'</span>should check 42 is 42<span class="pl-pds">'</span></span>, <span class="pl-k">function</span> () {
        <span class="pl-en">expect</span>(<span class="pl-c1">42</span>).<span class="pl-en">toEqual</span>(<span class="pl-c1">42</span>);
    });
});

});

Learn More