Mocking Neo4J GraphQL OGM in typescript / vitetest

I’m struggling to get a clean solution to mock the use of Noe4j GraphQL OGM using vitetest but cant quite work out the best way to do this cleanly..

I do not seem to be able to find a way to mock either the OGM generated <type>Model class, or the OGM.prototype.model function which is used to get the generated <type>Model using vanilla vitest.

I managed to solve this using vitest-mock-extended, but I feel/worry that this is a case where either I am missing some obvious way to do this with vanilla vitest, or, its a sign that I should be isolating the OGM parts and injecting more easily testable interface.

My questions are:

  1. Has anyone mocked OGM and is there an approach to mocking ogm.model using vanilla vitest (or equivalent framework) that I am missing?
  2. for mocking the generated Model classes (ie UserModel), again, is there an approach using vanilla vitest I am missing?
  3. have/would people recommend an alternative approach to create a wrapper/interface that is more easily testable / better suited for mocking and pass that into a version of UserSignup, and then have a UserSignupWithOGM(...) that takes OGM and does the ogm.model call passing in UserModel into the UserSignup (while I cant change the generated code, I’m happy isolating the rest of the codebase from this)

A simplified version of the setup is that I have a UserSignup that leverages a generated GraphQL function that OGM provides. so I have (simplified)

// UserSignup.ts

// UserSignup is registered as a resolver on the graphql server (apollo)
export const UserSignup =  async (
   _source: unknown, 
   { username, password }: { username: string, password: string }, 
   // ogm is passed in via context by the server
   { ogm } : { ogm: OGM<ModelMap> }) => {
  // ogm provides a typesafe way to get the class we can then use to exercise the graphQL API
  const User = ogm.model("User")
  const [existing] = await User.find({
      where: {
          username,
      },
  });
  if (existing) {
      throw new Error(`User with username ${username} already exists!`);
  }
  const { users } = await User.create({
      input: [
          {
              username,
              password,
          }
      ]
  });

  // assume a bunch more here ...

  return users[0].id;
}

In my test, I cannot seem to find a way to get a mocked version of ogm so that ogm.model("User") would give a mocked version of the UserModel
And given OGM is generating the code, cant change any types etc..
I also could not get vanilla vitest to give a mock on the UserModel class.

the best I have found so far is to use vitest-mock-extended (see below)

// UserSignup.test.ts
import { expect, test } from "vitest";
import { UserSignup } from "../UserSignup";
import { OGM } from "@neo4j/graphql-ogm";
import { UserModel, ModelMap } from "../../generated/ogm-types";
import { mockDeep, mock } from "vitest-mock-extended";

const UserModelMock = mock<UserModel>();

const ogm = mockDeep<OGM<ModelMap>>({ funcPropSupport: true });
ogm.model.calledWith("User").mockReturnValue(UserModelMock);

test("Test User Signup", async () => {
   UserModelMock.find
      .calledWith(any())
      .mockReturnValue(Promise.resolve([]);

   UserModelMock.create
      .calledWith(any())
      .mockReturnValue(Promise.resolve([ { username: 'test', password: '', id: 'new_user' }]));
   const userId = await UserSignup();
   expect(userId).toBe('new_user');
});

Here is the definition of OGM:

// @neo4j/graphql-ogm.d.ts
declare class OGM<ModelMap = unknown> {

   constructor(input: OGMConstructor);
    // etc
   model<M extends T extends keyof ModelMap ? ModelMap[T] : Model, T extends keyof ModelMap | string = string>(name: T): M;
   // etc
}

and UserModel:

// ../../generated/ogm-types.ts

export declare class UserModel {
  public find(args?: {
    where?: UserWhere;

    options?: UserOptions;
    selectionSet?: string | DocumentNode | SelectionSetNode;
    args?: any;
    context?: any;
    rootValue?: any;
  }): Promise<User[]>;
  public create(args: {
    input: UserInput[];
    selectionSet?: string | DocumentNode | SelectionSetNode;
    args?: any;
    context?: any;
    rootValue?: any;
  }): Promise<UsersMutationResponse>;
  public update(args: {
    where?: UserWhere;
    update?: UserUpdateInput;
    connect?: UserConnectInput;
    disconnect?: UserDisconnectInput;
    create?: UserCreateInput;
    connectOrCreate?: UserConnectOrCreateInput;
    selectionSet?: string | DocumentNode | SelectionSetNode;
    args?: any;
    context?: any;
    rootValue?: any;
  }): Promise<UpdateUsersMutationResponse>;
  public delete(args: {
    where?: UserWhere;
    delete?: UserDeleteInput;
    context?: any;
    rootValue?: any;
  }): Promise<{ nodesDeleted: number; relationshipsDeleted: number }>;
  public aggregate(args: {
    where?: UserWhere;

    aggregate: UserAggregateSelectionInput;
    context?: any;
    rootValue?: any;
  }): Promise<UserAggregateSelection>;
}

New contributor

SB217 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật