如何用玩笑来监视代理对象?

How to spyOn proxied objects with jest?

有一种情况,库将代理对象导出为 public API。鉴于库的 API 无法更改,我未能找到一种方法如何使用 jest 监视代理对象。这是一个例子:

describe("spying on proxied objects with Jest", () => {
    it("should pass", () => {
        const foo = {
            a() {
                return 42;
            }
        };

        const p = new Proxy(foo, {
            get() {
                return () => {
                    return 53;
                };
            }
        });

        const mock = jest.spyOn(foo, "a");
        p.a(); // 53
        expect(mock).toHaveBeenCalled();
    });
});

上面的测试用例失败了:

expect(jest.fn()).toHaveBeenCalled()

Expected number of calls: >= 1
Received number of calls:    0

有什么想法吗?

那是因为 foo.a 实际上从未被调用,因为您的代理不会调用您的目标。

本次测试通过

describe("Foo", () => {
    it("should pass", () => {
      const foo = {
        a() {
          return 42;
        },
      };

      const p = new Proxy(foo, {
        get(target, prop) {
          return () => {
            target[prop]();  /* <-- Calling target */
            return 52;
          };
        },
      });

      const mock = jest.spyOn(foo, "a");
      const result = p.a(); // 52
      expect(mock).toHaveBeenCalled();
    });
  });