2

TestFunction我正在尝试从接口在我的测试用例中实现以下方法Moacha

(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;
    }
4

1 回答 1

2

如果你看一下关键字的代码,it没有方法只接受一个函数(没有标题)。据我在Mocha 的文档中看到的,没有一次是it没有标题的关键字。因此,您的代码也应该使用它:

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

编辑:如果您查看DefinitelyTyped 存储库index.d.ts中的源代码,您可以看到,和关键字都引用了interface。该接口确实指定为有效签名,但在 Mocha 文档中找不到。ittestspecifyTestFunction(fn: AsyncFunc): Test;

我们可以看到,这个界面是2年前TestFunction添加的,替换了ITestDefinition原来ITestDefinition的,没有标题的功能是不允许使用的。引入这些更改的提交消息是“更强大的 Mocha 定义”。为什么要添加这个,我不知道。我怀疑这是由于另一个包,mocha-typescript也取决于DefinitiveTyped Typings,并且这个包可能需要(fn: Func): Test;签名才能被打字稿正确验证。

最终的结果是,Mocha 不允许it在没有标题的情况下使用。

于 2019-12-30T10:51:17.107 回答