node.js - 在 typescript 上扩展 super 测试

标签 node.js typescript testing bdd supertest

我正在尝试在 supertest 上创建一个扩展。

使用我在问题中发现的内容 Extending SuperTest .我在 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);
}

在测试块内,我可以使用:
request(app)
  .post('/user/settings')
  .authenticate(user)
  .send(...)

这工作正常。现在的问题是在 *.test.ts 中使用扩展名文件。

正如 Extend Express Request object using Typescript 中所建议的,我尝试创建一个文件以使用 typescript 功能 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.
  }
}

并且还改变了我的 tsconfig.json
{
  "compilerOptions": {

    ...

    "typeRoots": ["./src/types"],

    ...

  }
}

但是当我运行 npx tsc
$ npx tsc
src/api/user.test.ts:51:8 - error TS2551: Property 'authenticate' does not exist on type 'Test'.

51       .authenticate(user);
          ~~~~~~~

问题
有没有办法在 typescript 环境中解决这个问题?

[编辑]
额外信息(不一定有用):
在同一个项目中,我在 express 上有扩展名, chaipdf-merge-js .使用上述方法,所有这些都可以正常工作。
supertest有点奇怪也许是关于 @types/supertest那是阻止它工作。

这是我项目中的一小部分代码,这些代码已经可以用于 express 了:
// file location: ./src/types/express
import { ModelBase } from '../../models/base';

declare global {
  namespace Express {
    export interface Response {
      model: (model: ModelBase) => this;
    }
  }
}

最佳答案

我不确定为什么,但这对我有用:

declare module "supertest" {
  interface Test extends superagent.SuperAgentRequest {
    authenticate(user: any): this;
  }
}

结构:
index.ts
tsconfig.json
types
  - supertest
    - index.d.ts

我的 index.ts :
import request from "supertest";
import express from "express";

const app = express();

app.get("/user", function (req, res) {
  res.status(200).json({ name: "john" });
});

request(app)
  .post("/user/settings")
  .authenticate({ tokens: { token: "", xsrfToken: "" } })
  .send();

我的 tsconfig.json :
{
  "compilerOptions": {
    "outDir": "dist",
    "rootDir": ".",
    "typeRoots": ["./types", "./node_modules/@types"],
    "esModuleInterop": true
  },
  "files": ["./types/supertest/index.d.ts", "index.ts"]
}

我的 types/supertest/index.d.ts :
import superagent from "superagent";

declare module "supertest" {
  interface Test extends superagent.SuperAgentRequest {
    authenticate(user: any): this;
  }
}

我认为declare module "supertest"是关键部分。其他一切你都做对了。

关于node.js - 在 typescript 上扩展 super 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60959823/

相关文章:

node.js - Sails-React : Receive data on programmatic redirection

Angular 2 新路由器 : How to get router parameters of a child component?

javascript - 如何从 Protractor 返回对象

javascript - 如何从虚拟内存中为node.js提供html+js服务?

javascript - 如何使用await函数保存变量并在发生错误时使用回调?

javascript - 使用 MEAN 堆栈进行身份验证,无需用户系统

typescript - 根据另一个类型的可选属性构造具有所需属性的新类型

typescript - Electron ,Angular2, "fs"

c++ - 关于tcl测试的问题

php - Cakephp 测试数据库 - 模型问题