Mocha - 无法实现 TestFunction 接口方法

Mocha - unable to implement TestFunction interface methods

我正在尝试在我的测试用例中通过 Moacha

TestFunction 接口实现以下方法

(fn: Func): Test;

这里是实现

describe("testing get request", () => {
    it(() => {
        const res = request(app).get("/get-page");
        expect(res.status).to.equal(200);
    });
});

我得到的错误是这样的:

TypeError: Test argument "title" should be a string. Received type "function"

当我在 it 中的匿名函数之前添加标题时,它工作正常。知道为什么我无法实现只采用函数的方法。

界面来自index.d.ts

interface TestFunction {
        (fn: Func): Test;
        (fn: AsyncFunc): Test;
        (title: string, fn?: Func): Test;
        (title: string, fn?: AsyncFunc): Test;
        only: ExclusiveTestFunction;
        skip: PendingTestFunction;
        retries(n: number): void;
    }

如果您看一下 code for the it keyword, there is no method that accepts only a function (without a title). As far as I can see in the documentation of Mocha,没有一次 it 关键字没有标题。所以,你的代码也应该使用它:

describe("testing get request", () => {
    it("should return statuscode 200", () => {
        const res = request(app).get("/get-page");
        expect(res.status).to.equal(200);
    });
});

编辑:如果你看一下 index.d.ts in the DefinitelyTyped repository, you can see that the it, test and specify keywords all reference the TestFunction interface 的来源。该接口确实将 (fn: AsyncFunc): Test; 指定为有效签名,但在 Mocha 文档中找不到。

We can see that the TestFunction interface was added to replace the ITestDefinition some 2 years back, and the original ITestDefinition does not allow the use of a function without a title. The message of the commit introducing these changes is "More robust Mocha definitions". Why this was added, I don't know. I suspect that it is due to another package, mocha-typescript, also depending on the DefinitelyTyped typings,这个包可能需要 (fn: Func): Test; 签名才能被打字稿正确验证。

最后的结果是Mocha不允许it无标题使用