DrewML / mocha-classes

Write Mocha tests using ES2015 classes and ES2016 decorators
7 stars 2 forks source link

Context binding for functions in class #4

Open dewwwald opened 5 years ago

dewwwald commented 5 years ago

The gist of the issue

The problem can be reproduced by having a class with a function that calls another member function or tries to access a member variable on the class:

@bdd.describe('Class tests')
export class Tests {

  constructor() {}

  testHelper() {
    return Promise.resolve();
  }

  @bdd.it('should be able to find function')
  test1() {
    // this refers to some other object and not the class
    return this.testHelper();
  }
}

I am curious to know if this was intended or if there is a potential fix here. I am looking in to it, and will keep this thread updated, but I would like your input on this and confirmation on whether this is something you will be willing to fix or merge a PR for?

Further insights

I realize that at first glance this might not seem like something worth solving. My argument for fixing this instead of using a global variable is because the behaviour of class member functions are being modified. I am using a DI framework to inject dependencies into the class. Granted I could just store these injected dependencies on the global scope, but that just feels strange.

Another reason would be the use of helper functions on the member level that themselves will have access to the scope of the class. This seem to me to make sense.

dewwwald commented 5 years ago

For anyone looking for this feature, a temporary fix would be to do the following:

  1. subscribe to this thread to get updates
  2. make use of this temp package I created that merges #3 to the main branch (link -> https://github.com/BigTeamDotCo/mocha-classes ) I will keep updates there until the owner gets back to us.

Once merged I will post here that that package will be removed and add a depreciation warning to that package.

Published on npm under @bigteam/mocha-classes