piotrwitek / ts-mocha

Mocha thin wrapper that allows running TypeScript tests with TypeScript runtime (ts-node) to get rid of compilation complexity
MIT License
190 stars 25 forks source link

Test context is undefined #53

Open henhal opened 4 years ago

henhal commented 4 years ago

I have a test suite in plain JS and a project which is only somewhat migrated to TS. My tests works fine with regular mocha using the transpiled JS, but when using ts-mocha on the mixed js/ts code I get some difference in behaviour. Mainly, the context of the before/after functions is undefined.

Example:

before(async() => {
  // some setup code that uses the mocha test context through "this"
}

...
describe( ... )

When this is run with ts-mocha, this is undefined in before, after etc, with plain mocha this is set to the test context object. Am I doing something wrong or does ts-mocha not have equivalent semantics as mocha?

EDIT: I found that arrow functions are discouraged in mocha: https://mochajs.org/#arrow-functions and that changing to function solves the issue! However, the same code works in mocha but not in ts-mocha, so there's still a discrepancy?