I'm trying to create a extension on supertest.
Using what I found in question Extending SuperTest. I have this working example on javascript
:
const request = require('supertest');
const Test = request.Test;
Test.prototype.authenticate = function(user) {
const {token, xsrfToken} = user.tokens;
return this
.set('Authorization', `Bearer ${token}`)
.set('X-XSRF-TOKEN', xsrfToken);
}
And inside a test block I can use:
request(app)
.post('/user/settings')
.authenticate(user)
.send(...)
This works fine. The problem now is to use the extension in a *.test.ts
file.
As suggested in Extend Express Request object using Typescript, I try to create a file to use the typescript feature Declaration Merging.
// file location src/types/supertest
declare namespace supertest {
export interface Test {
authenticate(user: any): this; // I didn't put a type on user to simplify here.
}
}
and also changed my tsconfig.json
{
"compilerOptions": {
...
"typeRoots": ["./src/types"],
...
}
}
But when I run npx tsc
$ npx tsc
src/api/user.test.ts:51:8 - error TS2551: Property 'authenticate' does not exist on type 'Test'.
51 .authenticate(user);
~~~~~~~
Is there a way to fix this, use this aproach in typescript?
Aucun commentaire:
Enregistrer un commentaire