Backbone.Deferred.Model: Why the unittest is not run?

69 views Asked by At

Here is the code in question.

define (require) ->

  Backbone = require 'backbone'

  require 'backbone-deferred'

  class Data extends Backbone.Deferred.Model

    urlRoot: 'data'

    parse: (resp) ->
      resp.record or resp

    isValid: ->
      @get 'valid'

    # Newly introduced
    dataUrl: ->
      @get('data_url')?.replace /abc/, ''

And here is the unit test

define (require) ->

  Data         = require 'model/Data'

  describe 'dataurl', ->

    describe 'No change', ->
      data = new Data id: '123', name: 'Dummy', data_url: 'https://m/a.png'
      expect(data.dataUrl()).to.equal('https://m/a.png')

For some reason the test is not run by mocha.

I am using this html to drive the test

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Mocha Tests</title>
  <link rel="stylesheet" href="../bower_components/mocha/mocha.css" type="text/css" media="screen" title="Tests for {%= name %}" charset="utf-8">
</head>
<body>
  <div id="mocha"></div>
  <script src="../bower_components/mocha/mocha.js" type="text/javascript" charset="utf-8"></script>
  <script src="../bower_components/chai/chai.js" type="text/javascript" charset="utf-8"></script>

  <script src="../bower_components/requirejs/require.js" type="text/javascript" charset="utf-8"></script>
  <!-- Requirejs configuration -->
  <script src="../temp/local/scripts/config.js" type="text/javascript" charset="utf-8"></script>

  <script type="text/javascript" charset="utf-8">
    // Dumping console messages to /dev/null to avoid parsing error
    // when grunt:mocha_phantomjs is run
    if (navigator.userAgent.indexOf('PhantomJS') >= 0) {
      var log = console.log
      console.log = function(message) {
        if (message.indexOf('<') === 0) {
          log.apply(this, Array.prototype.slice.call(arguments))
        }
      }
    }

    // Mixpanel is not loaded for unit testing.
    // Redirecting mixpanel to /dev/null.
    window.mixpanel = {
      track: function() {}
    };

    require.config({
      baseUrl: "../temp/local/scripts",
      shim: {
        'chai-jquery': {
          deps: ['jquery']
        },
        'sinon': {
          exports: 'sinon'
        }
      },
      paths: {
        // Aliasing path to find the specs
        'specs': '../../specs',

        'sinon'      : '../../../bower_components/sinon/lib/sinon',
        'sinon-chai' : '../../../bower_components/sinon-chai/lib/sinon-chai',
        'chai-jquery': '../../../bower_components/chai-jquery/chai-jquery',
        'handlebars' : '../../../bower_components/handlebars/handlebars',

      }
    });

    runTests = function() {

      // Ensuring that jquery and chai-jquery are loaded
      require(
          ['jquery', 'chai-jquery', 'sinon-chai', 'handlebars'],
          function($, jqueryChai, sinonChai, Handlebars) {
        // Registering testing style in globals for easy access
        window.expect = chai.expect;
        chai.use(jqueryChai);
        chai.use(sinonChai)

        mocha.setup({
          ui: 'bdd',
          ignoreLeaks: true
        });

        // Protect from barfs
        console = window.console || function() {};

        // Don't track
        window.notrack = true;

        // Helper... not really needed but in case we want to do something fancy
        var runMocha = function() {
          mocha.run();
        };

        require([
          // Many other tests; removed for clarity
          'specs/data.spec'
        ], function() {
          // For xUnit and code coverage reports, we run the tests
          // using mocha-phantomjs runner.
          if (window.mochaPhantomJS) {
            mochaPhantomJS.run();
          }
          else {
            mocha.run();
          }
        });
      });

    }

  </script>
</body>
</html>

Here is the output of the grunt test task

Running "mocha:default" (mocha) task
Testing: test/index.html


  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․

  1081 passing (9s)

>> 1081 passed! (9.17s)

My questions are:

1) Number of test is changed at all after adding the new test. What are the possible reasons the new test is not picked up?

2) If I intentionally introduce an error in my unit test it will cause this in case of a test failure:

Warning: PhantomJS timed out, possibly due to a missing Mocha run() call. Use --force to continue. 

I am sure it is related to the test but there is not enough information to diagnose the problem. How can I turn on logging or obtain additional information?

1

There are 1 answers

0
Daniel J.G. On BEST ANSWER

Your test case should be defined using the it function instead of describe (which is used to group tests)

define (require) ->    
  Data = require 'model/Data'    
  describe 'dataurl', ->    
    it 'No change', ->
      data = new Data id: '123', name: 'Dummy', data_url: 'https://m/a.png'
      expect(data.dataUrl()).to.equal('https://m/a.png')

I have created this jdfiddle so you can see the difference with your original definition.

If you introduce an error in the test, if the test is not defined inside the it function then it will affect the whole test run. You can check it in this other fiddle (You will notice the second test doesn't run after the error in the wrong defined test)