Skip to content
This repository has been archived by the owner on Dec 27, 2018. It is now read-only.

Mocha's this context is reset between functions #62

Open
stroiman opened this issue Jul 29, 2016 · 2 comments
Open

Mocha's this context is reset between functions #62

stroiman opened this issue Jul 29, 2016 · 2 comments

Comments

@stroiman
Copy link

stroiman commented Jul 29, 2016

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
Copy link

jonmc12 commented Aug 6, 2016

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
Copy link

jsep commented Sep 29, 2016

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants