2
votes

I've been having trouble setting up a test framework for a NodeJS + Backbone app with the constant "require is not defined" error. I finally got it working using an in-browser test framework which picks up all of the dependencies I need and running a test.js file.

Currently, I'm only doing basic testing of my Backbone models, views, and collections. Now, I want to add in API testing but I'm back to the same "require is not defined" error. What is causing this? It's clear that I'm missing something fundamental here. I just want to add:

var request = require('supertest')
  , express = require('express');

var app = express();

Snippet of test.js:

describe('Application', function(){
  it("creates a global variable for the namespace", function() {
    should.exist(App);
  })
});


describe('Models', function() {

    describe('SearchFormModel', function() {
        beforeEach(function() {
            this.SearchFormModel = new App.Model.SearchFormModel();
            this.defaultFields = this.SearchFormModel.attributes;
        })

        it("created a SearchFormModel", function() {
            should.exist(this.SearchFormModel);
        })

        it("should have 7 default fields", function() {
            Object.keys(this.SearchFormModel).length.should.equal(7);
        })

        it("should default all fields to empty string", function() {
            for (var key in this.defaultFields) {
                this.defaultFields[key].should.equal("");
            }
        })
    });

});

test-runner.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <!-- Title &amp; Meta -->
    <title>Frontend tests</title>
    <meta charset="utf-8">

    <!-- Stylesheets -->
    <link rel="stylesheet" href="../node_modules/mocha/mocha.css">
</head>

<body>

    <div id="mocha"></div>

    <!-- Testing Libraries -->
    <script src="../node_modules/mocha/mocha.js"></script>
    <script src="../node_modules/chai/chai.js"></script>
    <script>
        // Use the expect version of chai assertions - http://chaijs.com/api/bdd
        var should = chai.should();

        // Tell mocha we want TDD syntax
        mocha.setup('tdd');
    </script>

    <!-- Libs -->
    <script src="../public/lib/jquery-1.8.2.min.js"></script>
    <script src="../public/lib/underscore-min.js"></script>
    <script src="../public/lib/backbone-min.js"></script>
    <script src="../public/lib/bootstrap.min.js"></script>
    <script src="../public/lib/highcharts.js"></script>
    <script src="../public/lib/bootstrap-datepicker.js"></script>
    <script src="../public/js/modules/exporting.js"></script>

    <!-- Source files -->
    <script src="../public/js/namespace.js"></script>
    <script src="../public/js/jst.js"></script>            
    <script src="../public/js/utils.js"></script>
    <script src="../public/js/models/models.js"></script>
    <script src="../public/js/models/search.js"></script>
    <script src="../public/js/models/plot.js"></script>
    <script src="../public/js/models/search_result.js"></script>
    <script src="../public/js/views/header.js"></script>
    <script src="../public/js/views/plot.js"></script>
    <script src="../public/js/views/list.js"></script>
    <script src="../public/js/views/search.js"></script>
    <script src="../public/js/router.js"></script>   
    <script src="../public/js/app.js"></script>    

    <!-- Test -->
    <script src="test.js"></script>

    <script>
        mocha.run();
    </script>

</body>
</html>
1

1 Answers

3
votes

require and commonjs only works in Node.js

If you run Browser test, then you need to code it like you'll run it in the browser. Also note that Unit Test should be done in isolation, you shouldn't need to load you app server (express) to run your test.

I'd like to point you to an easy solution from there, but there's just too many choices. Very basically, you should start running browser test in the browser by loading an html file.

Then, you'll want to automatize this and run browser test from the terminal. That's when you want to run test in PhantomJs and the likes and output browser results on the terminal. Around this, you can checkout Karma and Testem who're two browser test runner (remember here Mocha alone won't run browser test via command line).

As you're using Backbone, you might be interested in the Backbone-Boilerplate Karma + Grunt test setup as a starting point. See more on this here: https://github.com/backbone-boilerplate/backbone-boilerplate