Jest 的toEqual匹配器在检查相等性时会考虑空格。在测试中格式化预期值时,不可能以匹配包含换行符、制表符等的字符串的方式进行格式化。
Jest 是否提供了一种在匹配时忽略空格的方法?
注意:我编辑了问题以使其更通用。
我有一个使用 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 };
}
};
}
};
Run Code Online (Sandbox Code Playgroud)
我在我的测试中通过在describe函数内部进行引用:
beforeEach(() => {
jasmine.addMatchers(MyCustomMatchers);
});
Run Code Online (Sandbox Code Playgroud)
并在it函数中像这样使用:
expect(localValue).toBeTheSameAsRemote(remoteValue);
Run Code Online (Sandbox Code Playgroud)
测试运行正常,但打字稿编译器无法识别匹配器,这是有道理的,因为我没有在类型系统中的任何地方定义它
Property 'toBeTheSameAsRemote' does not exist on type 'JestMatchersShape<Matchers<void, MyType[]>, Matchers<Promise<void>, …Run Code Online (Sandbox Code Playgroud)