如何为自定义 chai 断言添加类型提示?
How to add typehint for custom chai assertion?
在 typescript 项目中,我为 chai 断言库创建了一个自定义断言,如下所示:
// ./tests/assertions/assertTimestamp.ts
import moment = require("moment");
import {Moment} from "moment";
const {Assertion} = require("chai");
const parseDateWithTime = (dateWithTime: string, format = "YYYY-MM-DD hh:mm:ss"): Moment => {
return moment.utc(dateWithTime, format);
};
const formatTimestamp = (timestampInMs: number): string => {
return moment.utc(timestampInMs).toISOString();
};
Assertion.addMethod("timestampOf", function (humanReadableDate: string, format = "YYYY-MM-DD hh:mm:ss") {
const expectedValue = parseDateWithTime(humanReadableDate, format);
const formatted = formatTimestamp(this._obj);
new Assertion(this._obj)
.to.eq(
expectedValue.valueOf(),
`\nEXPECTED: "${expectedValue.toISOString()}"\nGOT: "${formatted}"\n`,
);
},
);
我是这样使用的:
require("../assertions/assertTimestamp");
import {expect} from "chai";
describe("My test", () => {
it("should test timestamp", () => {
expect(1610841600000).to.be.timestampOf("2021-01-16");
});
});
但我收到打字稿错误:
TS2339:Property 'timestampOf' does not exist on type 'Assertion'.
我知道我可以通过以下方式解决此问题:
(expect(1610841600000).to.be as any).timestampOf("2021-01-16");
但我更想在 typescript 中注册我的方法,这样我也可以在我的 IDE.
中使用自动完成功能
在您的 assertTimestamp.ts
中,您可以添加:
declare global {
export namespace Chai {
interface Assertion {
timestampOf(date: string, format?: string): void;
}
}
}
它将添加您的自定义函数,同时保留现有函数。
如果您现在 运行 您的测试将会失败,如预期的那样:
AssertionError:
EXPECTED: "2021-01-16T00:00:00.000Z"
GOT: "2021-01-17T00:00:00.000Z"
: expected 1610841600000 to equal 1610755200000
Expected :1610755200000
Actual :1610841600000
在 typescript 项目中,我为 chai 断言库创建了一个自定义断言,如下所示:
// ./tests/assertions/assertTimestamp.ts
import moment = require("moment");
import {Moment} from "moment";
const {Assertion} = require("chai");
const parseDateWithTime = (dateWithTime: string, format = "YYYY-MM-DD hh:mm:ss"): Moment => {
return moment.utc(dateWithTime, format);
};
const formatTimestamp = (timestampInMs: number): string => {
return moment.utc(timestampInMs).toISOString();
};
Assertion.addMethod("timestampOf", function (humanReadableDate: string, format = "YYYY-MM-DD hh:mm:ss") {
const expectedValue = parseDateWithTime(humanReadableDate, format);
const formatted = formatTimestamp(this._obj);
new Assertion(this._obj)
.to.eq(
expectedValue.valueOf(),
`\nEXPECTED: "${expectedValue.toISOString()}"\nGOT: "${formatted}"\n`,
);
},
);
我是这样使用的:
require("../assertions/assertTimestamp");
import {expect} from "chai";
describe("My test", () => {
it("should test timestamp", () => {
expect(1610841600000).to.be.timestampOf("2021-01-16");
});
});
但我收到打字稿错误:
TS2339:Property 'timestampOf' does not exist on type 'Assertion'.
我知道我可以通过以下方式解决此问题:
(expect(1610841600000).to.be as any).timestampOf("2021-01-16");
但我更想在 typescript 中注册我的方法,这样我也可以在我的 IDE.
中使用自动完成功能在您的 assertTimestamp.ts
中,您可以添加:
declare global {
export namespace Chai {
interface Assertion {
timestampOf(date: string, format?: string): void;
}
}
}
它将添加您的自定义函数,同时保留现有函数。
如果您现在 运行 您的测试将会失败,如预期的那样:
AssertionError:
EXPECTED: "2021-01-16T00:00:00.000Z"
GOT: "2021-01-17T00:00:00.000Z"
: expected 1610841600000 to equal 1610755200000
Expected :1610755200000
Actual :1610841600000