Check Backbone/requireJs project with Jasmine

I have a project loaded using RequireJS and I would like to test the models.

I have the following test :

require(['../../js/models/lead_sharing'], function(Lead_sharing_Model) {
    describe('when instantiated', function () {
        it('should exhibit attributes', function() {
            var test = new Lead_sharing_Model({
                attribute : 3
            });
            expect(test.get('attribute')).toEqual(3);
        });
    });
});

But I have a error " failed with error: SyntaxError: Unexpected token"...

In reality , I don't know if it's possible to test a backbone/requireJs project with Jasmine. Indeed, how can I include my views/Models... without require config (like definition of the paths...)

Thanks you

Ps : just a small edit to specify that I would like test with a a js cmd. no in a browser. :)

Upvotes: 2

Views: 1848

Answers (1)

jmconrad
jmconrad

Reputation: 629

It is certainly possible to test a backbone/requirejs project with jasmine. Pull the same require config used by the app into the html page that drives the unit tests. For example:

<!doctype html>
<html lang="en">
<head>
  <link rel="stylesheet" href="vendor/jasmine.css">
</head>

<body>
  <script src="vendor/jasmine.js"></script>
  <script src="vendor/jasmine-html.js"></script>
  <script src="../assets/js/libs/jquery.js"></script>
  <script src="../assets/js/libs/underscore.js"></script>
  <script src="../assets/js/libs/backbone.js"></script>

  <!-- This points to the require config that is also used by the main app. -->
  <script data-main="../app/config" src="../assets/js/libs/require.js"></script>

  <script>
    require({ paths: { spec: "../test/spec" } }, [
      // Pull in all your modules containing unit tests here.
      "spec/lead-sharing-specs"
    ], function() {
      jasmine.getEnv().addReporter(new jasmine.TrivialReporter());
      jasmine.getEnv().execute();
    });
  </script>
</body>
</html>

Since you want to run this outside of the browser, check out PhantomJS, grunt, and grunt-jasmine-task.

Upvotes: 4

Related Questions