很难用玩笑来模拟supabase.eq().eq()

问题描述 投票:0回答:1

我有这个代码:

    const result = await req.supabase
      .from("some_table")
      .select()
      .eq("id", id1)
      .eq("landmarks.id", id2)
      .single();

我试图在我的测试环境中模拟它,但我一直无法缩小差距并使其正常工作——我不知道为什么。

我当前的实现:

  const mockSingle = jest.fn();

  jest.mock("@supabase/supabase-js", () => {
    return {
      createClient: jest.fn().mockImplementation(() => {
        return {
          select: jest.fn().mockReturnValue({
            eq: jest.fn().mockReturnValue({
              eq: jest.fn().mockReturnValue({
                single: mockSingle,
              }),
            }),
          }),
        };
      }),
    };
  });

with the following resolved value:

mockSingle.mockResolvedValueOnce({ data: { someData: [someData] }, error: null });

我不断收到此错误:

检索记录时出错 req.supabase.from(...).select(...).eq(...).eq 不是函数

编辑:我认为还值得一提的是,如果我从链中删除其中一个

.eq()
函数并删除等效的模拟
jest.fn()
,它确实会排队并工作。两岁的时候似乎发生了一些奇怪的事情
.eq().eq()

jestjs supabase ts-jest
1个回答
0
投票

您可以使用 mockReturnThis 函数来模拟链函数。

所以,你的模拟可能看起来像这样:

const mockSingle = jest.fn();

jest.mock("@supabase/supabase-js", () => {
  return {
    createClient: jest.fn().mockImplementation(() => {
      return {
        from: jest.fn().mockReturnThis(), // your chain start from `from()`
        select: jest.fn().mockReturnThis(),
        eq: jest.fn().mockReturnThis(),
        single: mockSingle,
      };
    }),
  };
});
© www.soinside.com 2019 - 2024. All rights reserved.