nestjs icon indicating copy to clipboard operation
nestjs copied to clipboard

Suggestion for more accurate types for nested objects

Open NickBolles opened this issue 1 year ago • 4 comments

fixes #885

export class MyClass {
  foo = {
    bar: () => {
      return 'foo.bar';
    },
  };
}

test('foo.bar', () => {
  const myMock = createMock<MyClass>();

  myMock.foo.bar.mockResolvedValue('foo.bar');
});
image

Based off the createMock source this nested object should also be a mock. Updating the typings as suggested fix this.

There are very likely other spots that need to be fixed too, I just looked at this specific error for now, if this is a good change I can look at the rest and add them to the PR.

NickBolles avatar Dec 16 '24 03:12 NickBolles

@NickBolles Are you able to add test coverage to fully expose this case? I'm trying to see which more scenarios this would capture and whether it may introduce hotspots to the typescript compiler

underfisk avatar Dec 23 '24 21:12 underfisk

Would love to see this improvement too since we know the mock exists but currently are working around it with code like -

(dbService.table.findAll as jest.Mock).mockResolvedValueOnce(data)

LMK if anything can be done to help.

JacobSiegle avatar Feb 14 '25 15:02 JacobSiegle

@NickBolles Are you able to provide some test coverage?

underfisk avatar Feb 22 '25 11:02 underfisk

@NickBolles Sorry for bumping, are you still maintaining the draft?

underfisk avatar Apr 21 '25 18:04 underfisk