-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proxy created with instance(mockedClass) gets "then" function -> Results in Timeout when Using Nestjs DI #219
Comments
We had the same problem. I was not aware of this ticket before, so I've analyzed everything on my own already before. The cause for the timeout is because NestJS under the hood calls await on resolved DI tokens (object). Since object (mock) contains the Why mocked object has One possible workaround is what you already mention: deleting export class TestUtil {
public static patchTsMockito(): void {
try {
const MockableFunctionsFinder = require('ts-mockito/lib/utils/MockableFunctionsFinder').MockableFunctionsFinder;
if (!MockableFunctionsFinder.prototype.isMockableOriginal) {
MockableFunctionsFinder.prototype.isMockableOriginal = MockableFunctionsFinder.prototype.isMockable;
MockableFunctionsFinder.prototype.isMockable = function (name: string): boolean {
if (['catch', 'then'].indexOf(name) >= 0) {
return false;
}
return this.isMockableOriginal(name);
};
}
} catch (error) {
console.warn('Failed to patch ts-mockito MockableFunctionsFinder', error);
}
}
} By this fix TestUtil.patchTsMockito() |
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.
Results in:
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!
Results in:
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']
The text was updated successfully, but these errors were encountered: