我正在编写自定义的Jest匹配器。在此匹配器中,我想使用Jest的一些功能来帮助我验证对象,但找不到如何导入它们。
例如,我想做
import { isEqual } from 'jest' // ??? How do I do this?
expect.extend({
toMatch(received: any, expexted: any) {
let pass = true;
pass = pass && isEqual(received.foo, expexted.foo);
pass = pass && customMatcher(received.bar, expexted.bar);
...
},
});
相反,我能做的最好的就是尝试像这样的期望
expect.extend({
toMatch(received: any, expexted: any) {
let pass = true;
try {
expect(received.foo).toEqual(expexted.foo);
} catch {
pass = false;
}
pass = pass && customMatcher(received.bar, expexted.bar);
...
},
});