0

I have a function and I am using dynamoose lib under the hood to make queries to database simpler:

export class MyService {
  private rep = new Rep<SomeModel>("sometable");

  public async get(id, user) {
    return await this.rep.instance
      .scan('itemId')
      .eq(id)
      .and()
      .where('belongsTo')
      .not()
      .eq(user)
      .exec();
  }
}

I wrote a test that should check if this method is exposed. I mocked this using this way:

jest.mock("../Rep", () => {
  return {
    Rep: jest.fn().mockImplementation(() => {
      return {
        instance: {
          scan: () => {
            return {
              eq: () => {
                return {
                  and: () => return {} // ... etc. until final exec() returns some object
                }
              }
            }
          }
        }
    });
  }
});

const result = await new MyService().get("testid", "testuser");
expect(result).toBeDefined();

It works, but I feel it could be done better, especially when I add new funtions. How to make it work in more elegant way?

fewfew
  • 231
  • 2
  • 10

0 Answers0