Jasmine cheat sheet - Matchers

by imehesz

HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<link rel="stylesheet" href="https://raw.github.com/esbie/jasmine-bootstrap/master/src/jasmine-bootstrap.css">
<script src="http://pivotal.github.com/jasmine/lib/jasmine.js"></script>
<script src="http://pivotal.github.com/jasmine/lib/jasmine-html.js"></script>
  <script type="text/javascript">
    (function() {
      var jasmineEnv = jasmine.getEnv();
      jasmineEnv.updateInterval = 1000;

      var htmlReporter = new jasmine.HtmlReporter();

      jasmineEnv.addReporter(htmlReporter);

      jasmineEnv.specFilter = function(spec) {
        return htmlReporter.specFilter(spec);
      };

      var currentWindowOnload = window.onload;

      window.onload = function() {
        if (currentWindowOnload) {
          currentWindowOnload();
        }
        execJasmine();
      };

      function execJasmine() {
        jasmineEnv.execute();
      }

    })();
  </script>

CSS

body { background-color: #eeeeee; padding: 0; margin: 5px; overflow-y: scroll; }

#HTMLReporter { font-size: 11px; font-family: Monaco, "Lucida Console", monospace; line-height: 14px; color: #333333; }
#HTMLReporter a { text-decoration: none; }
#HTMLReporter a:hover { text-decoration: underline; }
#HTMLReporter p, #HTMLReporter h1, #HTMLReporter h2, #HTMLReporter h3, #HTMLReporter h4, #HTMLReporter h5, #HTMLReporter h6 { margin: 0; line-height: 14px; }
#HTMLReporter .banner, #HTMLReporter .symbolSummary, #HTMLReporter .summary, #HTMLReporter .resultMessage, #HTMLReporter .specDetail .description, #HTMLReporter .alert .bar, #HTMLReporter .stackTrace { padding-left: 9px; padding-right: 9px; }
#HTMLReporter #jasmine_content { position: fixed; right: 100%; }
#HTMLReporter .version { color: #aaaaaa; }
#HTMLReporter .banner { margin-top: 14px; }
#HTMLReporter .duration { color: #aaaaaa; float: right; }
#HTMLReporter .symbolSummary { overflow: hidden; *zoom: 1; margin: 14px 0; }
#HTMLReporter .symbolSummary li { display: block; float: left; height: 7px; width: 14px; margin-bottom: 7px; font-size: 16px; }
#HTMLReporter .symbolSummary li.passed { font-size: 14px; }
#HTMLReporter .symbolSummary li.passed:before { color: #5e7d00; content: "\02022"; }
#HTMLReporter .symbolSummary li.failed { line-height: 9px; }
#HTMLReporter .symbolSummary li.failed:before { color: #b03911; content: "x"; font-weight: bold; margin-left: -1px; }
#HTMLReporter .symbolSummary li.skipped { font-size: 14px; }
#HTMLReporter .symbolSummary li.skipped:before { color: #bababa; content: "\02022"; }
#HTMLReporter .symbolSummary li.pending { line-height: 11px; }
#HTMLReporter .symbolSummary li.pending:before { color: #aaaaaa; content: "-"; }
#HTMLReporter .bar { line-height: 28px; font-size: 14px; display: block; color: #eee; }
#HTMLReporter .runningAlert { background-color: #666666; }
#HTMLReporter .skippedAlert { background-color: #aaaaaa; }
#HTMLReporter .skippedAlert:first-child { background-color:...

JavaScript

function helloWorld(){
    return "Hello world";
}

function saySomething( something ){
    return something;        
}

function summary(numbers){
    if( typeof(numbers) == 'number' )
    {            
        return numbers;
    }
    else if( numbers != null && typeof( numbers ) != "string" && numbers.length > 1 )
    {
        var retval = 0;
        for( var i=0; i<numbers.length;i++ )
        {
            retval += numbers[i];
        }
        
        return retval;
    }
    else
    {
        return 0;
    }
}

// tests

describe("Hello world", function(){
    it( "returns a string", function(){
        expect(typeof(helloWorld())).toEqual( 'string' );
    });
    
    it( "says hello", function(){
        expect(helloWorld()).toEqual( 'Hello world' );
    });
});

describe( "Say something", function(){
    it( "returns whataver is passed as param", function(){
        expect( saySomething("say something") ).toBe("say something");            
    });
});

describe( "summary", function(){
    it( "returns the sum of numbers in an array", function(){
        expect(summary(5)).toBe(5);
        expect(summary([1,2,3,4,5])).toBe(15);
        expect(summary("shouldbe0")).toBe(0);        
        expect(summary(0)).toBe(0);
        expect(summary({name:"test name should be 0"})).toBe(0);
        expect(summary(null)).toBe(0);        
    });
});