Independent Jasmine tests

(disclaimer: I’m getting started with testing)

I’m using velocity + jasmine, doing client integration tests. I want to test two different behavior that depend on a Session variable set accordingly to the url parameter. I’m struggling to write two tests that would be totally independent, as if two different users where doing them. Here is what I have:

describe('Case A', function() {
  beforeEach(function (done) {
    Router.go('home');
    this.deferAfterFlush(done);
  });
  beforeEach(waitForRouter);

  it("doesnt show the Signup button", function() {
    console.log('case A', Session.get('signupAuth'), $("#signup-link").length);
    expect($("#signup-link").length).toEqual(0);
  });
});

describe('Case B', function() {
  beforeEach(function(done) {
    Router.go('home', {}, {query: {signupCode: 'superCode'}});
    this.deferAfterFlush(done);
  });
  beforeEach(waitForRouter);

  it("shows the Signup button", function() {
    console.log('case B', Session.get('signupAuth'), $("#signup-link").length);
    expect($("#signup-link").length).toEqual(1);
  });
});

When there is a signupCode parameter, it is saved in the Session ‘signupAuth’ and I redirect to ‘/home’.
When rendering ‘home’, this Session variable is checked and $("#signup-link") is only created if the variable is set.
waitForRouter is from https://meteor-testing.readme.io/docs/jasmine-integration-tests-with-iron-router

It seems that those tests are not independent. The Session variable set in ‘Case B’ is accessible from ‘Case A’ and this screws up everything. (I can see that from the logs and I’m pretty sure my client code is working correctly)
Each test works well if the other is not running.

Am I doing something wrong? How can I make those two tests be independent, as if they were running in two separate mirrors?

Thanks a lot!

You need to reset the global session state after each test with Session.clear().

I see, thanks. I guess I should similarly clear global variables attached to window right?

It seems to be breaking some Meteor stuff: adding Session.clear(); as the first thing to do in beforeEach leads to

TypeError: Cannot read property 'false' of undefined
packages/reactive-dict.js:192:37: TypeError: Cannot read property 'false' of undefined
  at packages/reactive-dict.js:192:37
  at Function._.each._.forEach (packages/underscore.js:157:22)
  at _.extend.clear (packages/reactive-dict.js:190:7)

I’ll look into it

using

for (key in Session.keys) {
  Session.set(key, undefined);
}

instead seems to do the trick