4
votes

I got little problem with combination busterjs+requirejs+backbone, structure of my project:

js-src

--lib //jquery, require, etc

--views

--models

-app.js //require config and start of app

js (compiled same structure as above)

test

-buster.js

-require-config.js

-test-test.js

require-config.js:

require.config({
  baseUrl: 'js-src/',
  paths: {
    jquery: 'lib/jquery',
    jplugins: 'lib/jquery.plugins',
    underscore: 'lib/underscore',
    backbone: 'lib/backbone'
  },
  shim: {
    'backbone': {
      deps: ['underscore', 'jplugins'],
      exports: 'Backbone'
    },
    'jplugins': {
      deps: ['jquery']
    }
  }
});

typical file exept off that in lib:

define(function (require) {
    var $ = require('jquery'),
      Backbone = require('backbone'),
      otherElem = require('views/other'),
      View = Backbone.View.extend({
        el: '#el',

        initialize: function () {

        },

        showLinks: function (value) {

        },

        render: function ) {

        }

    });

    return View;
});

buster.js:

var config = module.exports;
config['browser-all'] = {
  autoRun: false,
  environment: 'browser',
  rootPath: '../',
  libs: [
    'js-src/lib/require.js',
    'test/require-config.js'
  ],
  sources: [
    'js-src/**/*.js'
  ],
  tests: [
    'test/*-test.js'
  ]
 // extensions: [
 //   require('buster-amd')
 // ]
};

test-test.js:

buster.spec.expose();
require(['views/View'], function (module) {
  describe("An AMD module", function () {
    it("should work", function () {
      expect(true).toEqual(true);
    });
  });
});

When i run it using buster test i get:

Uncaught exception: ./js-src/lib/require.js:192 Error: Script error
http://requirejs.org/docs/errors.html#scripterror
TypeError: uncaughtException listener threw error: Cannot read property 'id' of undefined
    at Object.uncaughtException (/usr/local/lib/node_modules/buster/node_modules/buster-test-cli/lib/runners/browser/progress-reporter.js:49:50)
    at notifyListener (/usr/local/lib/node_modules/buster/node_modules/buster-core/lib/buster-event-emitter.js:37:31)
    at Object.emit (/usr/local/lib/node_modules/buster/node_modules/buster-core/lib/buster-event-emitter.js:101:17)
    at Object.emitCustom (/usr/local/lib/node_modules/buster/node_modules/buster-test-cli/lib/runners/browser/remote-runner.js:283:14)
    at /usr/local/lib/node_modules/buster/node_modules/buster-test-cli/lib/runners/browser/remote-runner.js:89:16
    at /usr/local/lib/node_modules/buster/node_modules/buster-test-cli/node_modules/buster-capture-server/lib/pubsub-client.js:79:47
    at Object.trigger (/usr/local/lib/node_modules/buster/node_modules/buster-test-cli/node_modules/buster-capture-server/node_modules/faye/node/faye-node.js:383:19)
    at Object.distributeMessage (/usr/local/lib/node_modules/buster/node_modules/buster-test-cli/node_modules/buster-capture-server/node_modules/faye/node/faye-node.js:666:30)
    at Object._deliverMessage (/usr/local/lib/node_modules/buster/node_modules/buster-test-cli/node_modules/buster-capture-server/node_modules/faye/node/faye-node.js:1065:20)
    at Object.<anonymous> (/usr/local/lib/node_modules/buster/node_modules/buster-test-cli/node_modules/buster-capture-server/node_modules/faye/node/faye-node.js:1004:12)
Firefox 16.0, Linux: 

How to write proper test with that structure?

2

2 Answers

0
votes

It will help if you run the browser tests and check the outputs in the console. The error messages therein are usually much more expressive. You should also remove the autoRun directive from the buster configuration and re-enable the "buster-amd" extension.

0
votes

I'm new to Buster.js as of yesterday, but I'll add the following 4 part suggestion.

1.) Uncomment "extensions: [require('buster-amd')]" in your 'buster.js'

2.) Remove 'baseUrl' from your 'require.config'

3.) Explicitly set paths to your libs. For example "jplugins: 'lib/jquery.plugins'" would become "jplugins: 'js-src/lib/jquery.plugins'", this would also be needed for models, collections, views, and other files sitting on the 'js-src/' directory.

require.config({
  paths: {
    jquery: 'js-src/lib/jquery',
    views: 'js-src/lib/views',
    somerootfile: 'js-src/somerootfile'

4.) Change your test to be like this ...

describe('some test', function(run) {
    require(['models/your_model'], function(YourModel) {
        run(function() {
            it('should load', function() {
                var yourModel = new YourModel();
                    yourModel.set('cat', 'dog');
                expect(YourModel.get('cat')).toEqual('dog');
            });
        });
    });
});

The problem seems to be that the 'baseUrl' in 'require.config' confuse Buster.js and tell it to no long respect the 'rootPath' set in 'buster.js'.