I am writing a one-page JavaScript application using Backbone and Backbone.Marionette. I use AMD and RequireJS modules to help organize my code and manage dependencies. I also use Mocha as a testing framework for TDD / BDD.
Everything worked fine until I wanted to inject cigarette butts, mockery, and spies using Sinon.JS. After a lot of searching, I came across the test framework page on the WikiWiki and Squire.js wiki , which seemed to fit my needs. However, when I try to use Squire.js to load a module, Mocha unexpectedly reports global leaks for module dependencies. Report leaks if I download the module directly using Require.JS.
For example, the following test code does not call Mocha to report any leaks:
define(['app/app'], function(app) { describe('App', function() { it('Should define a \'header\' region', function() { expect(app.headerRegion).to.exist; }); it('Should define a \'main\' region', function() { expect(app.mainRegion).to.exist; }); }); return { name: "App" }; });
However, converting the code to use Squire.js as follows causes Mocha to report leaks for jQuery, Backbone, and Marionette (depending on app.js):
define(['Squire'], function(Squire) { describe('App', function() { var testContext = {}; beforeEach(function(done) { testContext.injector = new Squire(); testContext.injector.require(['app/app'], function(app) { testContext.app = app; done(); }); }); it('Should define a \'header\' region', function() { expect(testContext.app.headerRegion).to.exist; }); it('Should define a \'main\' region', function() { expect(testContext.app.mainRegion).to.exist; }); }); return { name: "App" }; });
What am I doing wrong? I am completely puzzled that Mocha does not report a leak with RequireJS, but does it with Squire.js. I also tried some other solutions, which I found in another https://stackoverflow.com/a/3129609/ , such as a user-defined function and testr.js, prior to Squire.js, and similar results. To date, I have not been able to find an example that uses Mocha, RequireJS, and Sinon.JS together.
I posted my current code base on GitHub in case there is some important information that I missed or something like that. This test can be found in test \ spec \ test.app.js.
Any help is appreciated. I would love to walk past the monkeys with my test setup and actually work on my application. Thanks in advance.