3

I'm trying to get started with Buster.js, and I installed both buster and buster-amd, but even so my use of Require.js is causing problems. My buster.js file looks like this:

var config = module.exports;
config["My tests"] = {
    autoRun: false,
    environment: "browser", // as opposed to "node"
    extensions: [require("buster-amd")],
    rootPath: "../",
    sources: ['ext/require/require.js'],
    tests: ["buster-test/*-test.js"]
};

and my test like this:

define(['buster-test/buster'
], function(buster) {
    buster.spec.expose(); // Make some functions global
    describe("A Fake Test", function () {
        it("can be instantiated", function () {
            console.log('test')
        });
    });
    buster.run()
});

But when I try to run the above, I get:

Uncaught exception: ./buster/load-all.js:1 Uncaught ReferenceError: require is not defined
TypeError: uncaughtException listener threw error: Cannot read property 'id' of undefined
    at Object.module.exports.uncaughtException (/usr/lib/node_modules/buster/node_modules/buster-test-cli/lib/runners/browser/progress-reporter.js:42:50)
    at notifyListener (/usr/lib/node_modules/buster/node_modules/buster-test-cli/node_modules/bane/lib/bane.js:49:35)
    at Object.object.emit (/usr/lib/node_modules/buster/node_modules/buster-test-cli/node_modules/bane/lib/bane.js:127:17)
    at Object.module.exports.bane.createEventEmitter.emitCustom (/usr/lib/node_modules/buster/node_modules/buster-test-cli/lib/runners/browser/remote-runner.js:289:14)
    at /usr/lib/node_modules/buster/node_modules/buster-test-cli/lib/runners/browser/remote-runner.js:92:16
    at PubSubClient.on._handler (/usr/lib/node_modules/buster/node_modules/buster-test-cli/node_modules/ramp/lib/pubsub-client.js:73:43)
    at Object.Faye.Publisher.trigger (/usr/lib/node_modules/buster/node_modules/buster-test-cli/node_modules/ramp/node_modules/faye/node/faye-node.js:385:19)
    at Object.Faye.extend.Set.Faye.Class.distributeMessage (/usr/lib/node_modules/buster/node_modules/buster-test-cli/node_modules/ramp/node_modules/faye/node/faye-node.js:668:30)
    at Object.Faye.Client.Faye.Class._deliverMessage (/usr/lib/node_modules/buster/node_modules/buster-test-cli/node_modules/ramp/node_modules/faye/node/faye-node.js:1070:20)
    at Object.Faye.Client.Faye.Class.receiveMessage (/usr/lib/node_modules/buster/node_modules/buster-test-cli/node_modules/ramp/node_modules/faye/node/faye-node.js:1007:12)

Has anyone seen anything like this before, and if so do you have any suggestions as to what I'm doing wrong?

P.S. If I remove the extensions: line I get a similar error, except that it complains about define instead of require. So it seems like the failure to find require is happening inside the plug-in ... but I have no idea how to provide Require to the plug-in.

machineghost
  • 33,529
  • 30
  • 159
  • 234

2 Answers2

4

Have you tried adding the require.js to libs instead of sources on your buster config? So the config would look like this:

var config = module.exports;
config["My tests"] = {
    autoRun: false,
    environment: "browser", // as opposed to "node"
    libs: [ 'ext/require/require.js' ],
    extensions: [require("buster-amd")],
    rootPath: "../",
    tests: ["buster-test/*-test.js"]
};
vitorbal
  • 2,871
  • 24
  • 24
  • 1
    To be perfectly honest, after trying Buster a few different times in the last year or so, and every time failing to get it going because of a new and different (un-helpful) error, I've come to realize that Buster just isn't ready for prime time. I've thus abandoned it in favor of Mocha (+Phantom for headless testing) and I couldn't be happier. Mocha "just works", out of the box, and while Phantom isn't quite as easy, it's still MUCH easier to get going than Buster. Hopefully this answer will help some other poor Buster user, but I can't confirm it since I've left for greener pastures. – machineghost Feb 15 '13 at 18:44
  • P.S. Just to be clear, I still have great hopes for Buster, as I loved the book its author wrote about Javascript testing. That guy (I forget his name) clearly knows what JS testing should look like ... I just wish he hadn't released his library in such an incomplete state. – machineghost Feb 16 '13 at 01:32
  • @machineghost Totally agree with you. Love the stuff I learned in "Test-Driven JavaScript Development" (by Christian Johansen, btw), but I have the same experience as you. Started using BusterJS, then got a mysterious error after upgrading, then that disappeared for another error, then I found out the node releases were lagging behind the actual releases, etc. ... I also found it was not ready for prime time yet. Too bad, because it has features that Mocha is far from having. I especially *hate* having to create the html files when testing code using mocha-phantomjs. – oligofren Oct 16 '13 at 15:05
  • BTW, they seem to have picked up the development pace a lot in recent months, and though lagging a bit behind their roadmap, they seem to be making lots of progress. But seeing the changelog and the amount of "Breaking changes" makes me a bit weary of using it until 1.0 starts coming closer ... It is still a bit of a moving target it seems. The day I can quit using Mocha+PhantomJS and just run everything in Buster will be a good one :) – oligofren Oct 16 '13 at 15:11
2

I take a different approach. I don't disable autorun, but instead use Buster's async test case format where you define the test case as a function that's passed a run callback. Use the (not well documented) resources: config setting to allow require to load your source code.

config["Browser tests"] = {
    environment: "browser",
    libs: [ 'test/require_config.js','require.js' ],
    rootPath: "../",
    resources: ["your_source_code/**/*.js"],
    tests: ["buster-test/*-test.js"]
};

Then use require() in your tests, and when you've loaded your code call the run callback with the tests:

buster.testCase("AppCode",function(run) {
  require(["appCode"],function(appCode) {
    run({
      "it works": function() { assert(true) }
    })
  });
});

I've created an example project showing this method require.js with buster.js. It has a small helper function to do both the testCase and require call simultaneously.

timruffs
  • 910
  • 8
  • 9