-
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
Fixed problem with a mock resolving a mocked value with Promises #194
base: master
Are you sure you want to change the base?
Conversation
By default, all symbols except .hasOwnProperty() are stubbed even if they are not assigned a value. This is to be able to track references to all invocations. However, the way that Promise.resolve() works, it checks for the presence of a .then() property and if it is there assumes that the resolve value is a promise so it tries to get its resolved value. But this is a mock reference so the stub does not return a resolved value so the promise never resolves. This fix excludes .then() and .catch() from automatically creating a stub so that Promise will not try to resolve it, but the user can still override the call with a when() clause. This fix also adds "Symbol(Symbol.toPrimitive)" to the exclude list so that by default toString() on a mock will return something other than null.
Added test to verify that adding "Symbol(Symbol.toPrimitive)" to the exclude list will allow toString() conversion to work properly.
In the prior commit, the location of one of the new tests was in the wrong location. This prevented the code from working and the coverage went down. Moved the test to the correct location and everything passes and the coverage went up-- at least on my computer.
Also commented on #191 hoping to bring visibility to this fix! |
Hi, when this is going to be merged in? |
Any update on this? |
Hi, when will this be merged? Loads of ignored tests at the moment :( haha |
@Napas @alex-e-c @alex-faber while waiting for the merge, as a workaround you can wrap the mocked object. not the best but at least you can run the tests. |
@MOHACGCG I'm pretty sure |
correct, what i meant is the alternatives do resolve, you can use them as a workaround for now. |
@MOHACGCG sorry my bad, I misread! I'll give it a go, thanks! |
no worries. good luck. I edited the comment to make it more clear! |
@Napas @alex-e-c @alex-faber @MOHACGCG The workaround from this comment #191 (comment) worked for me, with no need to alter the resolved value's structure. I hope it works for you as well. |
Any update on this? |
Any reason this hasn't been merged in? The workaround is fine, but this would be a good fix to have in the library. |
Same question :D "Any reason this hasn't been merged in? The workaround is fine, but this would be a good fix to have in the library." |
ola what's up with this? time to merge this bad boy |
Looks like we gotta fork and just publish it ourselves on npm ;') |
Thank you for this library! This issue is a real obstacle for new users and a nuisance for anyone using this library with nestjs. Could you please look into this github issue. A summary: NestJs thinks mocks are a promise and waits indefinitely. To fix this Thank you! |
Hi, thanks for PR. But wouldn't this break anything if someone would like to mock |
@NagRock I think there are at least two sides of the problem: 1. Class has own method
|
This workaround works for me: // This import is not that nice but it's the only way to create mocks that works with promises
import {Mocker} from "ts-mockito/lib/Mock";
export function mockForPromise(clazz: unknown, instance?: unknown): any {
return new Mocker(clazz, instance).getMock();
}
// use it
const mockApp = mockForPromise(Model<AppEntity>);
when(mockAppRepository.getAppByPackageId("test-package-id")).thenResolve(instance(mockApp)); |
@NagRock it is so painful for this to wait several years when a fix is right there in front of our eyes. If you've abandoned the project, can you at least appoint a contributor to take over? |
To respond to this: Yes. However, it's inevitable. There's no better way to do this when working with proxies. I think the benefits outweigh the downsides. Especially because mocking "then" and "catch" is unlikely - way more unlikely than working with promises. |
The long description is in #191 so this is short.
This is a replacement for #192 which was incorrect.
This fixes a problem where returning a mocked object through thenResolve() on another mocked object never resolves.