Squire breaks other tests

I use Karma, Jasmine, Jasmine. Asink, Sinon and Tea.

The good news is ... this test is working correctly. Dependence mocks, spies receive a call, and intentional violation of the test object leads to unsuccessful trials.

define(['chai', 'squire'], function (chai, Squire) { var should = chai.should(), async = new AsyncSpec(this), subject, injector = new Squire(); describe('EventsView', function () { describe('when an event is clicked', function () { var mockModel, stub; async.beforeEach(function (done) { setFixtures('<div id="screen"></div>'); mockModel = { toJSON: function () { return { dimensions: "hu1 vu2", events: [{ date: "8/29/2013", id: "8923", title: "Fancy Show", venue: "Lovely venue", }, { date: "8/29/2013", id: "9034", title: "Exciting Game", venue: "Lovely stadium" }], id: 3566, kind: "events", title: "Top events this week" }; }, fetch: function () {} }; stub = sinon.stub(); injector.mock('tiles/events-tile/events-detail-model', Squire.Helpers.constructs({ fetch: stub })); injector.require(["tiles/events-tile/events-view"], function (ev) { subject = new ev(mockModel); done(); }); }); async.afterEach(function (done) { injector.clean(); injector.remove(); done(); }); async.it('should attempt to fetch the event details', function (done) { $('#screen').html(subject.$el); $('.event').first().click(); stub.called.should.be.true; done(); }); }); }); }); 

The bad news ... the load of other tests that were previously excellent now fails for unknown reasons. For example: Error: Backbone.history has already been started as well as TypeError: 'undefined' is not an object (evaluating 'Backbone.Validation.mixin')

If I comment on the fragment

 injector.require(["tiles/events-tile/events-view"], function (ev) { subject = new ev(mockModel); done(); }); 

Then the rest of the tests work again. I used to have things like this before, and usually it was until the sinon recovered. Calling injector.clean() doesn't seem to provide the magic bullet I was hoping for.

+8
javascript requirejs karma-runner jasmine squirejs
source share
2 answers

The problem is the use of modules without AMD. If I remember correctly when Squire creates a new context, it will create a new instance of the non AMD module in the global namespace. As a result, your code and test will refer to another object. I think I managed to solve this problem by doing:

 var injector = new Squire(); injector.mock("backbone", function() { return Backbone; }); 

Squire should handle AMD modules better.

+3
source share

In my experience, Squire causes more headaches than it solves. Do you use any jQuery plugins? I found that they play poorly with the squire and can lead to unsuccessful results from subsequent tests. In this case, check out this blog on how to deal with it.

My recommendation, however, is to completely disrupt the squire. Using synon and creating your own utilities to determine exactly what you need should not only repeat your asynchronous tests synchronously (where the test code is synchronous), but it will also help to create more testable code. It should also help you understand your code and libraries at a deeper level. Obstacle Backbone.history to perform routing actions without actually starting routes is a great exercise and a fantastic utility to add to the test library.

+5
source share

All Articles