N
N
Nikolai Kupstas2020-01-30 14:37:57
JavaScript
Nikolai Kupstas, 2020-01-30 14:37:57

How to extend mocha context in typescript?

Hello. Actually, I have global hooks that create a piece of data and write it to this . Naturally, these variables remain untyped in my tests. I would like to fix this by extending the Context with d.ts

What I tried:

declare namespace Mocha {
    class Context {
        client: IClient;
        bus: IBus;
        testNumber: number;
        connectionParams: ConnectionParams;
    }
}


Also in tsconfig in the files section I added the path to this file. But I'm still not shown the type of these variables.
I know that the key [key:string]: any is described there , so I removed it to see the result more accurately and ts began to fall.

Maybe someone has already encountered this, because in my opinion it should be something standard when writing tests on ts

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robur, 2020-01-30
@Robur

it's easiest to type this in handlers
it( 'my test', function (this: MyContext) {})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question