Mocha Error - this.timeout Is Undefined
If you’re using an ES6 compiler like TypeScript or Babel, you may have run into an odd error when you tried to call this.timeout()
from your Mocha tests.
1 2 3 4 |
|
If you look at the compiled output, the source of the problem becomes evident.
The compiler is taking the value of this
from outside the test.
This is also the behavior you’d see if you used a JS engine with ES6 support.
1 2 3 4 5 |
|
Arrow functions specify that the scope of the “this” variable inside the function is the same as its scope outside the function.
Unfortunately, in this case, that isn’t what we want. We want “this” to be the Mocha object that we can call this.timeout()
on.
Switching back to the old-school function style fixes the problem:
1 2 3 4 |
|
And there you have it. Be careful with “arrow” functions in Mocha tests. They’re fine to use in most cases, but if you need to call this.timeout()
, make sure you switch back to the old-school function syntax.