Testing Backbone with JSDOM

333 Views Asked by At

I'm trying to figure out how to test backbone using jsdom and nodeunit as the testing framework.

I've got the error: "Error: Calling node's require("text") failed with error: Error: Cannot find module 'text'"

What means, that the context was not able to load requirejs-text plugin.

I've created the DOM context witn:

var assert = require('assert'); 
var fs = require('fs');
var index_html = fs.readFileSync(__dirname + '/../static/index.html', 'utf-8');
var requirejs = require('requirejs');
var jsdom = require('jsdom');
// require('amdefine/intercept');
var jQuery = require('jquery');

On the test setUp, on trying to open up the backbone view:

exports.setUp = function(callback){
    var document = jsdom.jsdom(index_html);
    var window = document.parentWindow;
    jsdom.getVirtualConsole(window).sendTo(console);

    requirejs.config({ 
      baseUrl: __dirname + '/',
      packages: [{
          name: 'text',
          location: '../node_modules/requirejs-text/text.js ',
          main: 'text.js'
        },
      ],
    });

    requirejs([__dirname + '/../lib/views/stackoverflow.js'], function(bb){
        callback();
    });
};

The exception is raised on before the callback is called. The view that I'm using is just a regular view using requirejs-text to load the template. I've also created a GitHub Repository just to better explain the issue. Just clone it, enter the project dir and type make. It should reproduce all the steps that I've did.

Thanks in advance!

0

There are 0 best solutions below