如何在不调用Expect的情况下直接使用Jest的匹配器?

时间:2019-10-25 22:46:27

标签: javascript npm jestjs

我正在编写自定义的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);

    ...
  },
});

0 个答案:

没有答案