-
-
Notifications
You must be signed in to change notification settings - Fork 16
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
fix(transformer): Ensure mocked interfaces don't extend themselves infinitely if passed as generic argument #312
fix(transformer): Ensure mocked interfaces don't extend themselves infinitely if passed as generic argument #312
Conversation
…finitely if passed as generic argument
Hi :). Thanks for starting a discussion about this annoying error. The solution you are applying to the generic declaration functionality will definitely work for that specific scenario but it may break for existing functionality that we support. I'll try to type here a code example of what it could break. class A {
propA: string
}
class C<T> {
propC: T
}
class B extends C<A> {
}
const a = createMock<A>();
const b = createMock<B>();
expect(b.propC.propA).toBe(""); If I am not mistaken propA will be undefined because the generic A will be already present in the declarationKey because already mocked I think we should add a test first to generic already been mocked by ts-auto-mock FYI |
I threw your example into the playground file and got the following output:
With these changes, But let me know if you have any tests in your WIP-branch that will make this fail. |
Hi :) You are right! I didn't read the code correctly at first. This would be a first good approach to just avoiding an infinite loop. However, we should document this unsupported behavior.
class C<T> {
propC: T
test: string
}
class A extends C<A> {
propA: number
}
const b = createMock<A>();
expect(b.propC.propC.test).toBe(""); // this will fail because we will not support generics of the same type Basically we would avoid checking any generic types that its the same of the type class that you are trying to mock. |
I suppose the proper way of implementing this, is to store the value of
to:
How are you currently approaching it? |
Thats precisesly how I am currently approaching this :) of course there are stell few things to finish |
I'll update the
Stay tuned :p |
By the way, I will gladly help out if you have your WIP branch lying around somewhere to fix this for good. |
Thats great!!! Thanks :) i should be able to openthe branch later or tomorrow morning :) |
As the title states, type arguments weren't considered in cases where the interface itself was passed as a generic argument, which would result in a circular extension. If they are indeed passed as a generic argument to the extending class or interface they will already have been defined (/mocked). These changes terminates early to make sure the logic doesn't continue forever.
This silences #183 and warns about the limitation instead.