practicalmeteor / meteor-mocha

Depreciated - Write meteor package tests with mocha and run them in the browser or from the command line with spacejam.
https://atmospherejs.com/practicalmeteor/mocha
Other
41 stars 13 forks source link

Mocha's `this` context is reset between functions #62

Open stroiman opened 8 years ago

stroiman commented 8 years ago

When adding data to mocha's context, the data disappears, or the context is reset. For example, I commonly use this pattern when testing node.js code, but it doesn't work in meteor:

beforeEach(function() {
  this.sinon = sinon.sandbox.create();
});

afterEach(function() {
  this.sinon.restore(); // here this.sinon is undefined
});

it('does something with sinon', function() {
  this.sinon.mock(obj).expct("foo"); // here this.sinon is undefined
});
jonmc12 commented 8 years ago

I'm seeing this issue when implementing Mocha's shared behaviors. However, I also have the issue when I use dispatch:mocha as the driver-package.

jsep commented 8 years ago

Hello @PeteProgrammer I was able to reproduce the problem only in the server, but something interesting happen when I used the arrow function ( () =>{} ) instead of the normal function, It works as expected, so as a workaround you can try use the arrow function (even so the official mocha documentation says no).

This is working for me:

beforeEach(() => {
  this.sinon = sinon.sandbox.create();
});

afterEach(() => {
  this.sinon.restore(); // here this.sinon is undefined
});

it('does something with sinon', =>() {
  this.sinon.mock(obj).expect("foo"); // here this.sinon is undefined
});

Maybe the next release is going to be fixed.