Skip to content

Proxy created with instance(mockedClass) gets "then" function -> Results in Timeout when Using Nestjs DIΒ #219

@ChrisWun

Description

@ChrisWun

I use ts-mockito to test my NestJs application. Basically it works very well, but there are problems if the word "then" is somewhere in the code. If the word "then" is present, the created proxy object is assigned a function "then". When using NestJs with Jest, this leads to a timeout when using the mock by overriding a provider for dependency injection.

export class Foo {
  myFunction(): Promise<string> {
    return Promise.resolve().then(() => {
      return 'foo';
    });
  }
}
const mockedFoo = mock(Foo);
const foo = instance(mockedFoo);

Results in:
Bildschirmfoto 2021-08-13 um 07 56 47

It is also very strange that the Promise functions (then, resolve) are also added as a function on the proxy when the corresponding code has been commented out, but is inside a block of a function!

export class Foo {
  myFunction(): Promise<string> {
    return Promise.resolve('foo');
    // return Promise.resolve().then(() => {
    //   return 'foo';
    // });
  }
}
const mockedFoo = mock(Foo);
const foo = instance(mockedFoo);

Results in:
Bildschirmfoto 2021-08-13 um 07 56 47

As a workaround to be able to use the mocks with NestJs Dependency Injection, I remove the then property from the proxy:
delete foo['then']

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions