DefinitelyTyped/types/supertest/index.d.ts
Ryspekov Ansar b4f5587ed2
🤖 Merge PR #45707 @types/supertest: Type declaration for agent() return type by @RayDarar
* Type declaration for agent() return type

## The case

In my project, it is essential to always clarify the variable type. In my case, writing a test suite would require to declare variables (so that they could be accessed in other scopes) and then later initialize them in a different block

## An example

```ts
describe("Health module", () => {
  // In this case I must declare variables here
  let app: NestApplication;
  let agent: any; // I could just copy and paste that huge return type, but it will break the code style. Using `any` is also a bad practice I guess

  beforeAll(() => {
    /* some code */
    // initialization goes here
   agent = supertest.agent(app.getHttpServer());
  });
  /* some code */
});
```
## Proposal

Declaring the return type of the agent function would be great, however, considering that I'm new to this library there may be a type declared already.

P.S. `SuperTestAgent` is just the first thing that I could come up with, it should be named more properly

* running lint
2020-06-29 10:13:15 -07:00

64 lines
2.1 KiB
TypeScript

// Type definitions for SuperTest v2.0.1
// Project: https://github.com/visionmedia/supertest
// Definitions by: Alex Varju <https://github.com/varju>
// Petteri Parkkila <https://github.com/pietu>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.0
import * as superagent from 'superagent';
export = supertest;
declare function supertest(app: any): supertest.SuperTest<supertest.Test>;
declare namespace supertest {
interface Response extends superagent.Response {}
interface Request extends superagent.SuperAgentRequest {}
type CallbackHandler = (err: any, res: Response) => void;
interface Test extends superagent.SuperAgentRequest {
app?: any;
url: string;
serverAddress(app: any, path: string): string;
expect(status: number, callback?: CallbackHandler): this;
expect(status: number, body: any, callback?: CallbackHandler): this;
expect(checker: (res: Response) => any, callback?: CallbackHandler): this;
expect(body: string, callback?: CallbackHandler): this;
expect(body: RegExp, callback?: CallbackHandler): this;
expect(body: Object, callback?: CallbackHandler): this;
expect(field: string, val: string, callback?: CallbackHandler): this;
expect(field: string, val: RegExp, callback?: CallbackHandler): this;
end(callback?: CallbackHandler): this;
}
interface AgentOptions {
ca?: any;
}
function agent(app?: any, options?: AgentOptions): SuperAgentTest;
type SuperAgentTest = SuperTest<Test> &
Pick<
Request,
| 'use'
| 'on'
| 'set'
| 'query'
| 'type'
| 'accept'
| 'auth'
| 'withCredentials'
| 'retry'
| 'ok'
| 'redirects'
| 'timeout'
| 'buffer'
| 'serialize'
| 'parse'
| 'ca'
| 'key'
| 'pfx'
| 'cert'
>;
interface SuperTest<T extends superagent.SuperAgentRequest> extends superagent.SuperAgent<T> {}
}