【发布时间】:2020-04-17 02:10:38
【问题描述】:
我有一个使用 jest 的 react/typescript 项目,其中我有一个自定义匹配器,例如:
export const MyCustomMatchers = {
toBeTheSameAsRemote: function(_util: any, _customEqualityTesters: any) {
return {
compare: function(actual: Brand, expected: RemoteBrand) {
const pass: boolean = attributesMatch(actual, expected);
const message: string = pass
? 'Local matches Remote'
: 'Local does not match Remote';
return { pass, message: () => message };
}
};
}
};
我在测试中通过在 describe 函数中引用它:
beforeEach(() => {
jasmine.addMatchers(MyCustomMatchers);
});
并在it 函数中像这样使用:
expect(localValue).toBeTheSameAsRemote(remoteValue);
测试运行正常,但 typescript 编译器无法识别匹配器,这是有道理的,因为我没有在类型系统的任何地方定义它
Property 'toBeTheSameAsRemote' does not exist on type 'JestMatchersShape<Matchers<void, MyType[]>, Matchers<Promise<void>, MyType[]>>'.ts(2339)
到目前为止,我发现与扩展 jasmine 和/或 jest 的命名空间有关,例如
declare namespace jasmine {
interface Matchers {
toBeTheSameAsRemote(remote: any): any;
}
}
这对我没有用。
你有什么想法吗?
【问题讨论】:
标签: typescript types jestjs ts-jest jasmine-matchers